custom/plugins/dkcProductoptions/src/Subscriber/CartPriceSubscriber.php line 72

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dkc\Productoptions\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  4. use Shopware\Core\Checkout\Cart\Event\CartChangedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator;
  7. use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. class CartPriceSubscriber implements EventSubscriberInterface
  10. {
  11.     private QuantityPriceCalculator $priceCalculator;
  12.     private RequestStack $requestStack;
  13.     public function __construct(
  14.         QuantityPriceCalculator $priceCalculator,
  15.         RequestStack $requestStack
  16.     ) {
  17.         $this->priceCalculator $priceCalculator;
  18.         $this->requestStack $requestStack;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             BeforeLineItemAddedEvent::class => ['onBeforeLineItemAdded'9999],
  24.             CartChangedEvent::class       => ['onCartChanged'9999],
  25.         ];
  26.     }
  27.     public function onBeforeLineItemAdded(BeforeLineItemAddedEvent $event): void
  28.     {
  29.         $lineItem $event->getLineItem();
  30.         $lineItemId $lineItem->getId();
  31.         $request $this->requestStack->getCurrentRequest();
  32.         if (!$request) {
  33.             return;
  34.         }
  35.         
  36.         $allParams $request->request->all();
  37.         
  38.         if (
  39.             isset($allParams['lineItems']) &&
  40.             isset($allParams['lineItems'][$lineItemId]) &&
  41.             isset($allParams['lineItems'][$lineItemId]['payload']) &&
  42.             isset($allParams['lineItems'][$lineItemId]['payload']['dkc_product_options'])
  43.         ) {
  44.             $options $allParams['lineItems'][$lineItemId]['payload']['dkc_product_options'];
  45.             $price $allParams['lineItems'][$lineItemId]['payload']['dkc_product_options_price'] ?? 0;
  46.             $labels $allParams['lineItems'][$lineItemId]['payload']['dkc_product_options_labels'] ?? null;
  47.             if (is_string($options)) {
  48.                 $options json_decode($optionstrue);
  49.             }
  50.             
  51.             if (is_string($labels)) {
  52.                 $labels json_decode($labelstrue);
  53.             }
  54.             
  55.             $lineItem->setPayloadValue('dkc_product_options'$options);
  56.             $lineItem->setPayloadValue('dkc_product_options_price', (float)$price);
  57.             
  58.             if ($labels) {
  59.                 $lineItem->setPayloadValue('dkc_product_options_labels'$labels);
  60.             }
  61.         }
  62.     }
  63.     public function onCartChanged(CartChangedEvent $event): void
  64.     {
  65.         $cart $event->getCart();
  66.         foreach ($cart->getLineItems() as $lineItem) {
  67.             if (
  68.                 !$lineItem->hasPayloadValue('dkc_product_options') ||
  69.                 !$lineItem->hasPayloadValue('dkc_product_options_price')
  70.             ) {
  71.                 continue;
  72.             }
  73.             
  74.             $optionPrice = (float)$lineItem->getPayloadValue('dkc_product_options_price');
  75.             $originalPrice $lineItem->getPrice();
  76.             
  77.             if ($optionPrice <= || !$originalPrice) {
  78.                 continue;
  79.             }
  80.             
  81.             $taxRules $originalPrice->getTaxRules();
  82.             $unitPrice $originalPrice->getUnitPrice() + $optionPrice;
  83.             $quantity $lineItem->getQuantity();
  84.             
  85.             $priceDefinition = new QuantityPriceDefinition(
  86.                 $unitPrice,
  87.                 $taxRules,
  88.                 $quantity
  89.             );
  90.             
  91.             $newPrice $this->priceCalculator->calculate($priceDefinition$event->getContext());
  92.             $lineItem->setPrice($newPrice);
  93.         }
  94.     }
  95. }