custom/plugins/FourtwosixThemeExtension/src/Subscriber/ProductListingSubscriber.php line 49

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixThemeExtension\Subscriber;
  3. use FourtwosixThemeExtension\Constant\Constant;
  4. use FourtwosixThemeExtension\Core\Content\FourtwosixStockPrice\FourtwosixStockPriceEntity;
  5. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  6. use Shopware\Core\Checkout\Cart\Price\Struct\ListPrice;
  7. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  8. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\Struct\ArrayStruct;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Shopware\Core\System\Tax\Aggregate\TaxRule\TaxRuleEntity;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ProductListingSubscriber implements EventSubscriberInterface
  18. {
  19.     protected EntityRepositoryInterface $ftsStockPrice;
  20.     protected EntityRepositoryInterface $taxRuleRepository;
  21.     protected SystemConfigService $systemConfigService;
  22.     public function __construct(
  23.         EntityRepositoryInterface $ftsStockPrice,
  24.         EntityRepositoryInterface $taxRuleRepository,
  25.         SystemConfigService $systemConfigService,
  26.     ) {
  27.         $this->ftsStockPrice $ftsStockPrice;
  28.         $this->taxRuleRepository $taxRuleRepository;
  29.         $this->systemConfigService $systemConfigService;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             ProductListingResultEvent::class => "onProductListingLoadedEvent",
  35.         ];
  36.     }
  37.     /**
  38.      * @param ProductListingResultEvent $event
  39.      * @return void
  40.      */
  41.     public function onProductListingLoadedEvent(ProductListingResultEvent $event)
  42.     {
  43.         $eventResults $event->getResult();
  44.         $productIds array_keys($eventResults->getElements());
  45.         $context $event->getSalesChannelContext();
  46.         $criteria = new Criteria();
  47.         $criteria->addFilter(new EqualsAnyFilter('productId'$productIds));
  48.         $stockPriceElements $this->ftsStockPrice->search($criteria$context->getContext())->getElements();
  49.         if (!$stockPriceElements) {
  50.             return;
  51.         }
  52.         $mainTaxId $this->systemConfigService->get('FourtwosixThemeExtension.config.ftsStockDiscountMandatoryFieldTaxId');
  53.         $activeBillingCountryId $context->getCustomer()?->getActiveBillingAddress()?->getCountryId();
  54.         // Default taxRate is 19 for DE
  55.         $taxRate 19;
  56.         if (!is_null($activeBillingCountryId)) {
  57.             $criteria = new Criteria();
  58.             $criteria->addFilter(new EqualsFilter('taxId'$mainTaxId));
  59.             $criteria->addFilter(new EqualsFilter('countryId'$activeBillingCountryId));
  60.             /** @var TaxRuleEntity $taxRatesPerCountry */
  61.             $taxRatesPerCountry $this->taxRuleRepository->search($criteria$context->getContext())->first();
  62.             $taxRate $taxRatesPerCountry->getTaxRate();
  63.         }
  64.         /** @var FourtwosixStockPriceEntity $stockPriceElement */
  65.         foreach ($stockPriceElements as $stockPriceElement) {
  66.             $productId $stockPriceElement->getProductId();
  67.             $stockProduct $stockPriceElement->getProduct();
  68.             /** @var SalesChannelProductEntity $product */
  69.             $product $eventResults->get($productId);
  70.             // Here recreate the listPrice for discount in FTS stock but not in promotion
  71.             $originalCalculatedPrice $product->getCalculatedPrice();
  72.             $unitPrice $originalCalculatedPrice->getUnitPrice();
  73.             // override if null ------------------------------------------------
  74.             $listPrice $originalCalculatedPrice->getListPrice();
  75.             if (!$listPrice) {
  76.                 $listPrice ListPrice::createFromUnitPrice($stockPriceElement->getNetPrice(), $unitPrice);
  77.             }
  78.             // Update the calculated price with new listPrice ------------------
  79.             $calculatedPrice = new CalculatedPrice(
  80.                 $unitPrice,
  81.                 $unitPrice,
  82.                 $originalCalculatedPrice->getCalculatedTaxes(),
  83.                 $originalCalculatedPrice->getTaxRules(),
  84.                 referencePrice$originalCalculatedPrice->getReferencePrice(),
  85.                 listPrice$listPrice,
  86.             );
  87.             $product->setCalculatedPrice($calculatedPrice);
  88.             $product->addExtension(Constant::FTS_STOCK_DISCOUNT_PLP_DISCOUNTED_PRICE,
  89.                 new ArrayStruct([
  90.                     'netPrice' => $stockPriceElement->getNetPrice(),
  91.                     'stock' => $stockProduct,
  92.                     'taxRate' => $taxRate,
  93.                 ]));
  94.         }
  95.     }
  96. }