custom/plugins/PixupCheapestPrice/src/Subscriber/ProductSubscriber.php line 104

Open in your IDE?
  1. <?php
  2. namespace PixupCheapestPrice\Subscriber;
  3. use Psr\Log\LoggerInterface;
  4. use Shopware\Core\Checkout\Cart\Price\CashRounding;
  5. use Shopware\Core\Content\Product\ProductEntity;
  6. use Shopware\Core\Content\Product\ProductEvents;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MinAggregation;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  15. use Shopware\Core\Framework\Struct\ArrayEntity;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ProductSubscriber implements EventSubscriberInterface
  18. {
  19.     private LoggerInterface $logger;
  20.     private EntityRepositoryInterface $productRepository;
  21.     private EntityRepositoryInterface $cheapestPriceProductRepository;
  22.     private EntityRepositoryInterface $cheapestPriceProductPriceRepository;
  23.     private CashRounding $rounding;
  24.     public function __construct(LoggerInterface $logger,
  25.                                 EntityRepositoryInterface $productRepository,
  26.                                 EntityRepositoryInterface $cheapestPriceProductRepository,
  27.                                 EntityRepositoryInterface $cheapestPriceProductPriceRepository,
  28.                                 CashRounding $rounding
  29.     )
  30.     {
  31.         $this->logger $logger;
  32.         $this->productRepository $productRepository;
  33.         $this->cheapestPriceProductRepository $cheapestPriceProductRepository;
  34.         $this->cheapestPriceProductPriceRepository $cheapestPriceProductPriceRepository;
  35.         $this->rounding $rounding;
  36.     }
  37.     /**
  38.      * @inheritDoc
  39.      */
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             ProductEvents::PRODUCT_WRITTEN_EVENT => 'productWritten',                       //product       | normal product
  44.             ProductEvents::PRODUCT_PRICE_WRITTEN_EVENT => 'productPriceWritten',            //product_price | advanced pricing
  45.         ];
  46.     }
  47.     public function productWritten(EntityWrittenEvent $event) {
  48.         if(is_null($event->getPayloads())) {
  49.             return;
  50.         }
  51.         foreach ($event->getPayloads() as $payload) {
  52.             try {
  53.                 if(isset($payload["id"])) {
  54.                     $product $payload['id'];
  55.                     foreach ($payload['price'] as $priceItem) {
  56.                         $currency $priceItem->getCurrencyId();
  57.                         $priceNet $this->rounding->cashRound($priceItem->getNet(), $event->getContext()->getRounding());
  58.                         $priceGross $this->rounding->cashRound($priceItem->getGross(), $event->getContext()->getRounding());
  59.                         //Get old price
  60.                         $criteria = new Criteria();
  61.                         $criteria->addFilter(new EqualsFilter('product'$product));
  62.                         $criteria->addFilter(new EqualsFilter('currency'$currency));
  63.                         $criteria->addSorting(new FieldSorting('activeFrom'FieldSorting::DESCENDING));
  64.                         $criteria->setLimit(1);
  65.                         $oldPrice $this->cheapestPriceProductRepository->search($criteria$event->getContext())->first();
  66.                         //if no old price OR old price !== new price
  67.                         if(!$oldPrice || (!($oldPrice['net'] == $priceNet && $oldPrice['gross'] == $priceGross))) {
  68.                             //If old price !== new price
  69.                             if(isset($oldPrice)) {
  70.                                 if (!(($oldPrice['net'] == $priceNet && $oldPrice['gross'] == $priceGross))) {
  71.                                     //End old price
  72.                                     $this->cheapestPriceProductRepository->update([
  73.                                         [
  74.                                             'id' => $oldPrice['id'],
  75.                                             'activeTill' => new \DateTime()
  76.                                         ]
  77.                                     ], $event->getContext());
  78.                                 }
  79.                             }
  80.                             //Insert new price
  81.                             $this->cheapestPriceProductRepository->create([[
  82.                                 'product' => $product,
  83.                                 'currency' => $currency,
  84.                                 'net' => $priceNet,
  85.                                 'gross' => $priceGross,
  86.                                 'activeFrom' => (new \DateTime())->format('Y-m-d H:i:s')
  87.                             ]], $event->getContext());
  88.                         }
  89.                     }
  90.                 }
  91.             } catch (\Exception $e) {}
  92.         }
  93.     }
  94.     public function productPriceWritten(EntityWrittenEvent $event) {
  95.         if(is_null($event->getPayloads())) {
  96.             return;
  97.         }
  98.         foreach ($event->getPayloads() as $payload) {
  99.             try {
  100.                 if(isset($payload["id"])) {
  101.                     $productPrice $payload['id'];
  102.                     if(isset($payload['price'])) {
  103.                         foreach ($payload['price'] as $priceItem) {
  104.                             $currency $priceItem->getCurrencyId();
  105.                             $priceNet $this->rounding->cashRound($priceItem->getNet(), $event->getContext()->getRounding());
  106.                             $priceGross $this->rounding->cashRound($priceItem->getGross(), $event->getContext()->getRounding());
  107.                             //End old price
  108.                             $criteria = new Criteria();
  109.                             $criteria->addFilter(new EqualsFilter('productPrice'$productPrice));
  110.                             $criteria->addFilter(new EqualsFilter('currency'$currency));
  111.                             $criteria->addSorting(new FieldSorting('activeFrom'FieldSorting::DESCENDING));
  112.                             $criteria->setLimit(1);
  113.                             $oldPrice $this->cheapestPriceProductPriceRepository->search($criteria$event->getContext())->first();
  114.                             //if no old price OR old price !== new price
  115.                             if (!$oldPrice || !($oldPrice['net'] == $priceNet && $oldPrice['gross'] == $priceGross)) {
  116.                                 //If old price !== new price
  117.                                 if(isset($oldPrice)) {
  118.                                     if (!($oldPrice['net'] == $priceNet && $oldPrice['gross'] == $priceGross)) {
  119.                                         //End old price
  120.                                         $this->cheapestPriceProductPriceRepository->update([
  121.                                             [
  122.                                                 'id' => $oldPrice['id'],
  123.                                                 'activeTill' => new \DateTime()
  124.                                             ]
  125.                                         ], $event->getContext());
  126.                                     }
  127.                                 }
  128.                                 //Insert new price
  129.                                 $this->cheapestPriceProductPriceRepository->create([[
  130.                                     'productPrice' => $productPrice,
  131.                                     'currency' => $currency,
  132.                                     'net' => $priceNet,
  133.                                     'gross' => $priceGross,
  134.                                     'activeFrom' => (new \DateTime())->format('Y-m-d H:i:s')
  135.                                 ]], $event->getContext());
  136.                             }
  137.                         }
  138.                     }
  139.                 }
  140.             } catch (\Exception $e) {}
  141.         }
  142.     }
  143. }