custom/plugins/XioniXconfig/src/Core/Checkout/Cart/Subscriber/CheckoutCartSubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Xioni\Xconfig\Core\Checkout\Cart\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Cart;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  5. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  6. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemRemovedEvent;
  7. use Shopware\Core\Checkout\Cart\Exception\LineItemNotFoundException;
  8. use Shopware\Core\Checkout\Cart\Exception\LineItemNotRemovableException;
  9. use Shopware\Core\Checkout\Cart\Exception\PayloadKeyNotFoundException;
  10. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  11. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. class CheckoutCartSubscriber implements EventSubscriberInterface
  16. {
  17.     private $requestStack;
  18.     
  19.     public function __construct(RequestStack $requestStack) {
  20.         $this->requestStack $requestStack;
  21.     }
  22.     
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             BeforeLineItemAddedEvent::class => 'onLineItemAdded'
  27.         ];
  28.     }
  29.     
  30.     public function onLineItemAdded(BeforeLineItemAddedEvent $event): void
  31.     {
  32.         $lineItem $event->getLineItem();
  33.         if($this->requestStack->getCurrentRequest()->get('lineItems') != null) {
  34.             foreach ($this->requestStack->getCurrentRequest()->get('lineItems') as $key =>  $item) {
  35.                 if ($lineItem->getReferencedId() == $key) {
  36.                     if (isset($item['xconfig']) && $item['xconfig'] == "1" ) {
  37.                         $lineItem->setPayloadValue('xconfig'$item['xconfig']);
  38.                         if (isset($item['xconfigDesc'])) {
  39.                             $lineItem->setPayloadValue('xconfigDesc'$item['xconfigDesc']);
  40.                         }
  41.                         if (isset($item['xGroupIds']) && isset($item['xGroupValues'])) {
  42.                             $lineItem->setPayloadValue('xGroupIds'$item['xGroupIds']);
  43.                             $lineItem->setPayloadValue('xGroupValues'$item['xGroupValues']);
  44.                             $id 0;
  45.                             $productNumber $item['xProductNumber'];
  46.                             $optionIds explode(","$item['xGroupIds']);
  47.                             $valueIds explode(","$item['xGroupValues']);
  48.                             $configuration $this->getConfiguration($id$productNumber$optionIds$valueIds);
  49.                             $xPrice $configuration->variant_product_price1->raw;
  50.                             $xProductNumber $configuration->variant_product_productnumber;
  51.                             $xProductDescription $configuration->config_product_description;
  52.                             $xEan $configuration->variant_product_ean;
  53.                             $lineItem->setPayloadValue('xPrice'$xPrice);
  54.                             $lineItem->setPayloadValue('xProductNumber'$xProductNumber);
  55.                             $lineItem->setPayloadValue('xEan'$xEan);
  56.                             $lineItem->setPayloadValue('xProductDescription'$xProductDescription);
  57.                             $lineItem->setPayloadValue('xMedia'$item['xconfigMedia']);
  58.                         }
  59.                         
  60.                     }
  61.                     if (isset($item['xconfig_accessory']) && $item['xconfig_accessory'] == "1" ) {
  62.                         $lineItem->setPayloadValue('xconfig_accessory'$item['xconfig_accessory']);
  63.                         $id 0;
  64.                         $lineItem->setPayloadValue('variant_relation'$item['xconfig_accessory_variant_relation']);
  65.                     }                        
  66.                 }
  67.             }            
  68.         }
  69.     }
  70.     
  71.     public function getConfiguration($id$productNumber$optionIds$valueIds)
  72.     {
  73.         $url        'https://xconfig003.com/mvc_structure2/get_configuration_poller.php';
  74.         $data       = array('id' => $id'productnumber' => $productNumber'option_ids' => $optionIds'value_ids' => $valueIds);
  75.         $options    = array(
  76.             'http' => array(
  77.                 'header'  => "Content-type: application/x-www-form-urlencoded",
  78.                 'method'  => 'POST',
  79.                 'content' => http_build_query($data)
  80.             )
  81.         );
  82.         $context                stream_context_create($options);
  83.         $configuration          file_get_contents($urlfalse$context);
  84.         $configuration             json_decode($configuration);
  85.         
  86.         return $configuration;
  87.     }
  88. }