custom/plugins/TdsMerwareDiscounts/src/Core/Content/Product/SalesChannel/SalesChannelProductSubscriber.php line 89

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Tds\MerwareDiscounts\Core\Content\Product\SalesChannel;
  3. use Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator;
  4. use Shopware\Core\Checkout\Cart\Price\Struct\PriceCollection;
  5. use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  8. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Tds\MerwareDiscounts\Core\Checkout\Cart\Price\DiscountPriceCalculator;
  13. use Tds\MerwareDiscounts\Service\DiscountService;
  14. /**
  15.  * Class SalesChannelProductSubscriber
  16.  *
  17.  * @package Tds\MerwareDiscounts\Core\Content\Product\SalesChannel
  18.  */
  19. class SalesChannelProductSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var DiscountPriceCalculator
  23.      */
  24.     private $discountPriceCalculator;
  25.     /**
  26.      * @var QuantityPriceCalculator
  27.      */
  28.     private $quantityPriceCalculator;
  29.     /**
  30.      * @var SystemConfigService
  31.      */
  32.     private $systemConfigService;
  33.     /**
  34.      * @var DiscountService
  35.      */
  36.     private $discountService;
  37.     /**
  38.      * SalesChannelProductSubscriber constructor.
  39.      * #
  40.      * @param DiscountPriceCalculator $priceCalculator
  41.      * @param QuantityPriceCalculator $quantityPriceCalculator
  42.      * @param SystemConfigService $systemConfigService
  43.      * @param DiscountService $discountService
  44.      */
  45.     public function __construct(
  46.         DiscountPriceCalculator $priceCalculator,
  47.         QuantityPriceCalculator $quantityPriceCalculator,
  48.         SystemConfigService $systemConfigService,
  49.         DiscountService $discountService
  50.     ) {
  51.         $this->discountPriceCalculator $priceCalculator;
  52.         $this->quantityPriceCalculator $quantityPriceCalculator;
  53.         $this->systemConfigService $systemConfigService;
  54.         $this->discountService $discountService;
  55.     }
  56.     /**
  57.      * {@inheritDoc}
  58.      */
  59.     public static function getSubscribedEvents()
  60.     {
  61.         return [
  62.             'sales_channel.product.loaded' => 'loaded',
  63.         ];
  64.     }
  65.     /**
  66.      * Event function to trigger storefront price calculations
  67.      *
  68.      * @param SalesChannelEntityLoadedEvent $event
  69.      */
  70.     public function loaded(SalesChannelEntityLoadedEvent $event): void
  71.     {
  72.         /** @var SalesChannelProductEntity $product */
  73.         foreach ($event->getEntities() as $product) {
  74.             $this->calculatePrices(
  75.                 $event->getSalesChannelContext(),
  76.                 $product,
  77.                 $event->getSalesChannelContext()->getCustomer()
  78.             );
  79.         }
  80.     }
  81.     /**
  82.      * Function to calculate storefront prices (called per product).
  83.      *
  84.      * @param SalesChannelContext $context
  85.      * @param SalesChannelProductEntity $product
  86.      * @param CustomerEntity|null $customer
  87.      */
  88.     private function calculatePrices(SalesChannelContext $contextSalesChannelProductEntity $product$customer null): void
  89.     {
  90. // [{"artcode": "SW10001", "price": "40"}, {"artcode": "SW10002", "price": "30"}]
  91.         if (!empty($customer)) {
  92.             $generalDiscount $this->discountService->getGeneralDiscount($customer$product);
  93.             $productDiscountPrice $this->discountService->getProductDiscountPrice($customer$product);
  94.             if ($product->getCalculatedPrices()) {
  95.                 $priceCollection = new PriceCollection();
  96.                 foreach ($product->getCalculatedPrices() as $price) {
  97.                     $definition = new QuantityPriceDefinition(
  98.                         $price->getUnitPrice(),
  99.                         $price->getTaxRules(),
  100.                         $price->getQuantity()
  101.                     );
  102.                     /**
  103.                      * discount price: remove list prices (from/to)
  104.                      */
  105.                     if (!empty($productDiscountPrice)) {
  106.                         $discountPrice $this->discountPriceCalculator->createCalculatePrice($definition$context$generalDiscount$productDiscountPrice);
  107.                     } else {
  108.                         /**
  109.                          * no discount price: add (possible) general customer discounts
  110.                          */
  111.                         $discountPrice $this->discountPriceCalculator->createCalculatePrice($definition$context$generalDiscount);
  112.                     }
  113.                     $priceCollection->add($discountPrice);
  114.                 }
  115.                 $product->setCalculatedPrices($priceCollection);
  116.             }
  117.             $definition = new QuantityPriceDefinition(
  118.                 $product->getCalculatedPrice()->getUnitPrice(),
  119.                 $product->getCalculatedPrice()->getTaxRules(),
  120.                 $product->getCalculatedPrice()->getQuantity()
  121.             );
  122.             /**
  123.              * discount price: remove list prices (from/to)
  124.              */
  125.             if (!empty($productDiscountPrice)) {
  126.                 $discountPrice $this->discountPriceCalculator->createCalculatePrice($definition$context$generalDiscount$productDiscountPrice);
  127.                 $cheapestDiscountPrice $this->discountPriceCalculator->createCheapestPrice($definition$context$generalDiscount$productDiscountPrice);
  128.             } else {
  129.                 /**
  130.                  * no discount price: add (possible) general customer discounts
  131.                  */
  132.                 $discountPrice $this->discountPriceCalculator->createCalculatePrice($definition$context$generalDiscount);
  133.                 $cheapestDiscountPrice $this->discountPriceCalculator->createCheapestPrice($definition$context$generalDiscount);
  134.             }
  135.             $product->setCalculatedPrice($discountPrice);
  136.             $product->setCalculatedCheapestPrice($cheapestDiscountPrice);
  137.         }
  138.     }
  139. }