custom/plugins/EconsorAtBitConfiguratorConnector/src/Subscriber/AtBitSessionCleaner.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Econsor\Shopware\AtBitConfiguratorConnector\Subscriber;
  3. use Shopware\Core\Checkout\Cart\CartEvents;
  4. use Shopware\Core\Checkout\Cart\Event\LineItemQuantityChangedEvent;
  5. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  6. use Shopware\Core\Checkout\Cart\Event\LineItemRemovedEvent;
  7. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  8. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  9. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTax;
  10. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
  11. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRule;
  12. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  13. use Shopware\Core\Framework\Struct\ArrayStruct;
  14. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  15. use Shopware\Storefront\Page\PageLoadedEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Session\Session;
  18. class AtBitSessionCleaner implements EventSubscriberInterface
  19. {
  20.     private $session;
  21.     public function __construct(
  22.         Session $session
  23.     )
  24.     {
  25.         $this->session $session;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             LineItemRemovedEvent::class => 'onLineItemRemoved',
  31.             LineItemQuantityChangedEvent::class => 'onQuantityChanged',
  32.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced'
  33.         ];
  34.     }
  35.     public function onLineItemRemoved(LineItemRemovedEvent $event): void
  36.     {
  37.         $URI $_SERVER['REQUEST_URI'];
  38.         if(str_contains($URI'/delete')) {
  39.             $id explode('/'$URI);
  40.             $id array_pop($id);
  41.             $config $this->session->has('atBitConfiguratorProducts') ? $this->session->get('atBitConfiguratorProducts') : null;
  42.             if($config && in_array($idarray_keys($config))) {
  43.                 unset($config[$id]);
  44.                 $this->session->set('atBitConfiguratorProducts'$config);
  45.             }
  46.         }
  47.     }
  48.     public function onQuantityChanged(LineItemQuantityChangedEvent $event)
  49.     {
  50.         if($configuratorProduct $this->getLineItemFromSession($event->getLineItem()->getId())) {
  51.             $quantity $event->getLineItem()->getQuantity();
  52.             /** @var ArrayStruct $extension */
  53.             $extension $configuratorProduct->getExtension('atbitConfiguration');
  54.             $netPrice $extension->get('GesamtpreisOhneMwSt');
  55.             $tax $extension->get('MwSt');
  56.             $newPrice $this->getCalculatedPrice($netPrice$tax$quantity);
  57.             $configuratorProduct->setQuantity($quantity);
  58.             $configuratorProduct->setPrice($newPrice);
  59.             $this->setLineItemFromSession($configuratorProduct$event->getLineItem()->getId());
  60.         }
  61.     }
  62.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event)
  63.     {
  64.         $this->session->remove('atBitConfiguratorProducts');
  65.     }
  66.     private function getLineItemFromSession(string $id): ?LineItem
  67.     {
  68.         if(
  69.             ($lineItems $this->session->get('atBitConfiguratorProducts')) &&
  70.             array_key_exists($id$lineItems)
  71.         ) {
  72.             return $lineItems[$id];
  73.         }
  74.         return null;
  75.     }
  76.     private function setLineItemFromSession(LineItem $lineItemstring $id)
  77.     {
  78.         if($lineItems $this->session->get('atBitConfiguratorProducts')) {
  79.             $lineItems[$id] = $lineItem;
  80.             $this->session->set('atBitConfiguratorProducts'$lineItems);
  81.         }
  82.     }
  83.     private function getCalculatedPrice(float $netPricefloat $taxint $quantity): CalculatedPrice
  84.     {
  85.         $netPrice $netPrice $quantity;
  86.         $taxAmount $netPrice * ($tax 100);
  87.         $grossPrice $netPrice $taxAmount;
  88.         $calculatedTaxes = new CalculatedTaxCollection([
  89.             new CalculatedTax($taxAmount$tax$netPrice)
  90.         ]);
  91.         $taxRules = new TaxRuleCollection([
  92.             new TaxRule($tax)
  93.         ]);
  94.         return new CalculatedPrice(
  95.             ($grossPrice $quantity),
  96.             $grossPrice,
  97.             $calculatedTaxes,
  98.             $taxRules,
  99.             $quantity
  100.         );
  101.     }
  102. }