vendor/crehler/mojebambino-engine/src/Subscriber/ProductListResultSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. declare (strict_types=1);
  3. namespace Crehler\MojeBambinoEngine\Subscriber;
  4. use Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator;
  5. use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
  6. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRule;
  7. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  8. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  9. use Shopware\Core\Content\Product\ProductEntity;
  10. use Shopware\Core\Content\Product\SalesChannel\Price\ProductPriceCalculator;
  11. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ProductListResultSubscriber implements EventSubscriberInterface
  17. {
  18.     private EntityRepository $productRepository;
  19.     private QuantityPriceCalculator $quantityPriceCalculator;
  20.     public function __construct(
  21.         EntityRepository        $productRepository,
  22.         QuantityPriceCalculator $quantityPriceCalculator
  23.     )
  24.     {
  25.         $this->productRepository $productRepository;
  26.         $this->quantityPriceCalculator $quantityPriceCalculator;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             ProductListingResultEvent::class => 'onProductListingResult',
  32.         ];
  33.     }
  34.     public function onProductListingResult(ProductListingResultEvent $event): void
  35.     {
  36.         try {
  37.             $products $event->getResult()->getEntities();
  38.             /** @var SalesChannelProductEntity $product */
  39.             foreach ($products as $product) {
  40.                 $parentId $product->getParentId();
  41.                 if (!$parentId && $product->getChildCount()) {
  42.                     $parentId $product->getId();
  43.                 }
  44.                 if (!$parentId) {
  45.                     continue;
  46.                 }
  47.                 $this->calculateVersionPrices($event$product$parentId);
  48.             }
  49.         } catch (\Throwable $exception) {
  50.             dd($exception);
  51.         }
  52.     }
  53.     private function calculateVersionPrices(ProductListingResultEvent $eventSalesChannelProductEntity $productEntitystring $parentId): void
  54.     {
  55.         $criteria = new Criteria([$parentId]);
  56.         $criteria->getAssociation('children')
  57.             ->addFilter(new EqualsFilter('active'true));
  58.         /** @var SalesChannelProductEntity $parentProduct */
  59.         /** @var ProductEntity $child */
  60.         $parentProduct $this->productRepository->search($criteria$event->getContext())->first();
  61.         foreach ($parentProduct->getChildren() as $child) {
  62.             $taxRulesCollection = new TaxRuleCollection();
  63.             $taxRule = new TaxRule($child->getTax()->getTaxRate());
  64.             $taxRulesCollection->set($child->getTax()->getTaxRate(), $taxRule);
  65.             $priceDefinition = new QuantityPriceDefinition(
  66.                 $child->getPrice()->getCurrencyPrice($event->getContext()->getCurrencyId())->getNet(),
  67.                 $taxRulesCollection
  68.             );
  69.             $calculatedPrice $this->quantityPriceCalculator->calculate($priceDefinition$event->getSalesChannelContext());
  70.             $productEntity->getCalculatedPrices()->add($calculatedPrice);
  71.         }
  72.         $productEntity->getCalculatedPrices()->sort(fn($price1$price2) => $price1->getUnitPrice() < $price2->getUnitPrice() ? -1);
  73.     }
  74. }