# Vererbung wiederherstellen funktioniert nicht

**URL:** https://forum.shopware.com/t/vererbung-wiederherstellen-funktioniert-nicht/98495
**Category:** Programmierung
**Created:** [10. Februar 2023 um 16:14 UTC](https://forum.shopware.com/t/vererbung-wiederherstellen-funktioniert-nicht/98495 "2023-02-10T16:14:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![vertic4l](https://avatars.discourse-cdn.com/v4/letter/v/919ad9/32.png) [@vertic4l](https://forum.shopware.com/u/vertic4l)
#### Post date: [10. Februar 2023 um 16:14 UTC](https://forum.shopware.com/t/vererbung-wiederherstellen-funktioniert-nicht/98495/1 "2023-02-10T16:14:45Z")

</div>

Nach Migration der Daten wurde festgestellt dass Preise und Vererbungen fehlerhaft sind.  
Um dies nun zu beheben hab ich ein Plugin geschrieben:

```auto
<?php

declare(strict_types=1);

namespace PricingFixer\Commands;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;

class PricingFixerCommand extends Command
{

    protected function configure(): void
    {
        $this->setName("fix:pricing")->setDescription("fixes pricing of main products, removes prices from variants");
    }

    protected EntityRepositoryInterface $productRepository;

    public function __construct(
        EntityRepositoryInterface $productRepository,
    ) {
        parent::__construct();

        $this->productRepository = $productRepository;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->restoreVariantsInheritance();
        $output->writeln("done.");

        return 0;
    }

    protected function restoreVariantsInheritance()
    {
        $context = Context::createDefaultContext();
        $context->setConsiderInheritance(true);

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter("productNumber", "X2020")); // <- Beispiel-Produkt, sonst ("parentId", null)

        $products = $this->productRepository->search(
            $criteria,
            Context::createDefaultContext()
        )->getEntities();

        foreach ($products as $product) {
            $variants = $this->getProductVariants($product)->getEntities();

            foreach ($variants as $variant) {
                $product = [
                    "id" => $variant->getUniqueIdentifier(),
                    "price" => [[
                        "currencyId" => Defaults::CURRENCY,
                        "gross" => 0,
                        "net" => 0,
                        "linked" => true,
                    ]]
                ];

                $this->productRepository->update([$product], $context);
            }
        }
    }

    protected function getProductVariants(ProductEntity $product): EntitySearchResult
    {
        $parentProduct = $product->getParent();

        if ($parentProduct != null) {
            $product = $parentProduct;
        }

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter(
            "parentId",
            $product->getUniqueIdentifier()
        ));

        return $this->productRepository->search($criteria, Context::createDefaultContext());
    }
}

```

Wie zu sehen, wird hierbei **gross** und **net** auf 0 gesetzt da diese nicht benötigt werden.  
Die Property **linked** sollte allerdings dafür sorgen, dass die Variante wieder vom Hauptprodukt die Preise vererbt bekommt.

Der Code scheint zu funktionieren, eine Produkt Indexierung wird ausgelöst, aber die Vererbung wird einfach nicht wiederhergestellt.

Kann mir irgendjemand bitte sagen, was das verdammte Probleme hier ist? 😃

---

<div class="post-metadata">

### Author: ![vertic4l](https://avatars.discourse-cdn.com/v4/letter/v/919ad9/32.png) [@vertic4l](https://forum.shopware.com/u/vertic4l)
#### Post date: [10. Februar 2023 um 16:55 UTC](https://forum.shopware.com/t/vererbung-wiederherstellen-funktioniert-nicht/98495/2 "2023-02-10T16:55:08Z")

</div>

In der Version 6.4.19 von Shopware kann ich gerade über das Backend nicht mal die Vererbung wieder entfernen.

Fehler „No price for default currency defined“…

 ![Bildschirm­foto 2023-02-10 um 17.54.33](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/3X/e/7/e72d85c1e2443f7fb70e4ce6f1b8eacd6a549993.png)

Scheinbar hat Shopware selbst so seine Probleme damit.

---

<div class="post-metadata">

### Author: ![vertic4l](https://avatars.discourse-cdn.com/v4/letter/v/919ad9/32.png) [@vertic4l](https://forum.shopware.com/u/vertic4l)
#### Post date: [10. Februar 2023 um 16:59 UTC](https://forum.shopware.com/t/vererbung-wiederherstellen-funktioniert-nicht/98495/3 "2023-02-10T16:59:26Z")

</div>

Über das Backend kann ich manuell „Vererbung wiederherstellen“ und speichern.  
„Vererbung entfernen“ geht manuell aber nicht. Siehe Fehlermeldung von oben.

Im Plugin selbst, kann ich die Vererbung entfernen und sehe die Änderung im Backend.  
Die Vererbung wiederherstellen geht allerdings nicht über das Plugin, obwohl es sich nur um ein Wechsel von **false** zu **true** anscheinend handeln dürfte.

---

<div class="post-metadata">

### Author: ![vertic4l](https://avatars.discourse-cdn.com/v4/letter/v/919ad9/32.png) [@vertic4l](https://forum.shopware.com/u/vertic4l)
#### Post date: [10. Februar 2023 um 17:21 UTC](https://forum.shopware.com/t/vererbung-wiederherstellen-funktioniert-nicht/98495/4 "2023-02-10T17:21:40Z")

</div>

Hat sich erledigt. Einfach das machen was das Backend auch macht.

price =\> null genügt.
