custom/plugins/ApiSee/src/Subscriber/PageSubscriber.php line 54

Open in your IDE?
  1. <?php
  2. namespace SeeliaApiPlugin\Subscriber;
  3. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
  5. use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
  6. use Shopware\Core\Framework\Uuid\Uuid;
  7. //use Shopware\Core\Checkout\Cart\Price\Struct\PriceCollection;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\PriceCollection;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price;
  10. use Shopware\Core\Checkout\Cart\Delivery\Struct\Delivery;
  11. use Shopware\Core\Checkout\Cart\Delivery\Struct\DeliveryCollection;
  12. use Shopware\Core\Checkout\Cart\Delivery\Struct\DeliveryDate;
  13. use Shopware\Core\Checkout\Cart\Delivery\Struct\DeliveryPositionCollection;
  14. use Shopware\Core\Checkout\Cart\Delivery\Struct\ShippingLocation;
  15. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  16. use Shopware\Core\Checkout\Customer\CustomerEvents;
  17. use Shopware\Core\Checkout\Shipping\ShippingMethodEntity;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  19. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  22. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  24. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  25. class PageSubscriber implements EventSubscriberInterface
  26. {
  27.     private $request;
  28.     private $config;
  29.     private $deliveryRepository;
  30.     public function __construct($request$config$deliveryRepository)
  31.     {
  32.         $this->request $request;
  33.     $this->config $config;
  34.     $this->deliveryRepository $deliveryRepository;
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             GenericPageLoadedEvent::class => 'onLoad',
  40.             BeforeLineItemAddedEvent::class => 'onAdd',
  41.             //AfterLineItemAddedEvent::class => 'after'
  42.         ];
  43.     }
  44.     /**
  45.      * Get sid for api of server
  46.      * @param GenericPageLoadedEvent $event
  47.      */
  48.     public function onLoad(GenericPageLoadedEvent $event)
  49.     {
  50.         // only retrieve konfigurator session when logged in
  51.         if (!empty($event->getSalesChannelContext()->getCustomer())) {
  52.             $customerNumber $event->getSalesChannelContext()->getCustomer()->getCustomerNumber();
  53.             $ch curl_init("https://konfigurator.seelia.net/serverapi/custom/shop_usersession.php?token=${_ENV['KONFIGURATOR_TOKEN']}&id=${customerNumber}");
  54.             curl_setopt($chCURLOPT_TIMEOUT5);
  55.             curl_setopt($chCURLOPT_RETURNTRANSFER1);
  56.             $result curl_exec($ch);
  57.             $event->getPage()->assign(['api' => json_decode($result)]);
  58.             curl_close($ch);
  59.         }
  60.     }
  61.     /**
  62.      * @param BeforeLineItemAddedEvent $event
  63.      */
  64.     public function onAdd(BeforeLineItemAddedEvent $event)
  65.     {
  66.         if ($this->request->getCurrentRequest()->get('new-order')){
  67.             $date date("m/d/Y");
  68.             $imgBack $this->request->getCurrentRequest()->get('img-back');
  69.             $imgFront $this->request->getCurrentRequest()->get('img-front');
  70.             $imgGood $this->request->getCurrentRequest()->get('img-good');
  71.             $price $this->request->getCurrentRequest()->get('new-price');
  72.             $configId $this->request->getCurrentRequest()->get('config-id');
  73.             $configDate $this->request->getCurrentRequest()->get('config-date');
  74.             $offerGlb $this->request->getCurrentRequest()->get('offer-glb');
  75.             $offerPdf $this->request->getCurrentRequest()->get('offer-pdf');
  76.             $baoName $this->request->getCurrentRequest()->get('bao_name');
  77.             $doorWidth $this->request->getCurrentRequest()->get('door_width');
  78.             $doorHeight $this->request->getCurrentRequest()->get('door_height');
  79.         $cost null;
  80.             if ($this->request->getCurrentRequest()->get('shippingCost')){
  81.                 $cost $this->request->getCurrentRequest()->get('shippingCost');
  82.             }
  83.             $offerGlb explode('media'$offerGlb);
  84.             $offerGlb '/media'.$offerGlb[count($offerGlb) - 1];
  85.             $lineItem $event->getLineItem();
  86.             $lineItem->setPayloadValue('new_price'$price);
  87.             $lineItem->setPayloadValue('added_date'$date);
  88.             $lineItem->setPayloadValue('img_front'$imgFront);
  89.             $lineItem->setPayloadValue('img_back'$imgBack);
  90.             $lineItem->setPayloadValue('img_good'$imgGood);
  91.             $lineItem->setPayloadValue('config_id'$configId);
  92.             $lineItem->setPayloadValue('config_date'$configDate);
  93.             $lineItem->setPayloadValue('offer_glb'$offerGlb);
  94.             $lineItem->setPayloadValue('offer_pdf'$offerPdf);
  95.             $lineItem->setPayloadValue('bao_name'$baoName);
  96.             $lineItem->setPayloadValue('doorWidth'$doorWidth);
  97.             $lineItem->setPayloadValue('doorHeight'$doorHeight);
  98.         //if ($cost){
  99.             //    $lineItem->setPayloadValue('cost', $cost);
  100.             //}
  101.         $cart $event->getCart();
  102.             $referencedId $lineItem->getReferencedId();
  103.             $payload $lineItem->getPayload();
  104.         //dd(count($cart->getLineItems()), $cart->get($referencedId));
  105.             if ($cart->get($referencedId) && count($cart->getLineItems()) > && $this->request->getCurrentRequest()->get('new-price')){
  106.                 $label $cart->get($referencedId)->getLabel();
  107.         $id Uuid::randomHex();
  108.                 $newLineItem = new LineItem($id'product'$referencedId1);
  109.         $newLineItem->setRemovable(true);
  110.         $newLineItem->setStackable(false);
  111.                 $payload['productId'] = $referencedId;
  112.                 $newLineItem->setPayload($payload);
  113.                 $newLineItem->setLabel($label);
  114.                 $newPrice = +$this->request->getCurrentRequest()->get('new-price');
  115.                 $price = new CalculatedPrice($newPrice$newPrice$cart->getPrice()->getCalculatedTaxes(), $cart->getPrice()->getTaxRules(), );
  116.                 $newLineItem->setPrice($price);
  117.         $cart->getLineItems()->add($newLineItem);
  118.                 $cart->getLineItems()->remove($lineItem->getId());
  119.                 //$cart->addLineItems(new LineItemCollection([$newLineItem]));
  120.             }elseif($cart->get($referencedId) && count($cart->getLineItems()) <= && $this->request->getCurrentRequest()->get('new-price')){
  121.                 $label $cart->get($referencedId)->getLabel();
  122.                 $id Uuid::randomHex();
  123.                 $newLineItem = new LineItem($id'product'$referencedId1);
  124.                 $newLineItem->setRemovable(true);
  125.                 $newLineItem->setStackable(false);
  126.                 $payload['productId'] = $referencedId;
  127.                 $newLineItem->setPayload($payload);
  128.                 $newLineItem->setLabel($label);
  129.                 $newPrice = +$this->request->getCurrentRequest()->get('new-price');
  130.                 $price = new CalculatedPrice($newPrice$newPrice$cart->getPrice()->getCalculatedTaxes(), $cart->getPrice()->getTaxRules(), );
  131.                 $newLineItem->setPrice($price);
  132.                 $cart->setLineItems(new LineItemCollection([$newLineItem]));
  133.         }
  134.         }else{
  135.             $lineItems $this->request->getCurrentRequest()->get('lineItems');
  136.             if(!empty($lineItems) && isset($lineItems[$event->getLineItem()->getId()]['new_price']))
  137.             {
  138.                 $price $lineItems[$event->getLineItem()->getId()]['new_price'];
  139.                 $imgBack $lineItems[$event->getLineItem()->getId()]['img-back'];
  140.                 $imgFront $lineItems[$event->getLineItem()->getId()]['img-front'];
  141.                 $imgGood $lineItems[$event->getLineItem()->getId()]['img-good'];
  142.                 $configId $lineItems[$event->getLineItem()->getId()]['config_id'];
  143.                 $offerGlb $lineItems[$event->getLineItem()->getId()]['offer_glb'];
  144.                 $offerPdf $lineItems[$event->getLineItem()->getId()]['offer_pdf'];
  145.                 if (array_key_exists('bao_name'$lineItems[$event->getLineItem()->getId()])){
  146.                     $baoName $lineItems[$event->getLineItem()->getId()]['bao_name'];
  147.                 }
  148.         $cost null;
  149.                 if (array_key_exists('shippingCost'$lineItems[$event->getLineItem()->getId()])){
  150.                 $cost $this->request->getCurrentRequest()->get('shippingCost');
  151.                 }
  152.                 $offerGlb explode('media'$offerGlb);
  153.                 $offerGlb '/media/'.$offerGlb[count($offerGlb) - 1];
  154.                 $event->getLineItem()->setPayloadValue('new_price'$price);
  155.                 $event->getLineItem()->setPayloadValue('img_front'$imgFront);
  156.                 $event->getLineItem()->setPayloadValue('img_back'$imgBack);
  157.                 $event->getLineItem()->setPayloadValue('img_good'$imgGood);
  158.                 $event->getLineItem()->setPayloadValue('config_id'$configId);
  159.                 $event->getLineItem()->setPayloadValue('offer_glb'$offerGlb);
  160.                 $event->getLineItem()->setPayloadValue('offer_pdf'$offerPdf);
  161.                 if (isset($baoName)){
  162.                     $event->getLineItem()->setPayloadValue('bao_name'$baoName);
  163.                 }
  164.         //if ($cost){
  165.                 //$lineItem->setPayloadValue('cost', $cost);
  166.                 //}
  167.             }
  168.         }
  169.     if($this->request->getCurrentRequest()->get('shippingCost')){
  170.         $cost $this->request->getCurrentRequest()->get('shippingCost');
  171.         //if ($cost){
  172.             //    $lineItem->setPayloadValue('cost', $cost);
  173.             //}
  174.     }
  175.     }
  176.     public function after(AfterLineItemAddedEvent $event)
  177.     {
  178.     $lineItems $event->getLineItems();
  179.         $cost = [];
  180.         foreach ($lineItems as $lineItem){
  181.             if(array_key_exists('cost'$lineItem->getPayload())){
  182.                 $cost[] = +$lineItem->getPayload()['cost'];
  183.             }
  184.         }
  185.         
  186.         $cost array_sum($cost);
  187.     $key 'shipping-method-' $this->config->get('SeeliaApiPlugin')['config']['salesId'];
  188.             $shippingMethodPrices $event->getCart()->getData()->get($key)->getPrices()->first();
  189. $price = new CalculatedPrice($cost$cost$event->getCart()->getPrice()->getCalculatedTaxes() ,$event->getCart()->getPrice()->getTaxRules());
  190.     dd($price);
  191.             //$price = new Price($event->getContext()->getCurrencyId(), $cost, $cost, false);
  192.             $shippingMethodPrices->setCurrencyPrice(new PriceCollection([$price]));
  193.         dd($event->getCart()->getData()->get($key));
  194.     }
  195. }