<?php
namespace SeeliaApiPlugin\Subscriber;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
use Shopware\Core\Framework\Uuid\Uuid;
//use Shopware\Core\Checkout\Cart\Price\Struct\PriceCollection;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\PriceCollection;
use Shopware\Core\Framework\DataAbstractionLayer\Pricing\Price;
use Shopware\Core\Checkout\Cart\Delivery\Struct\Delivery;
use Shopware\Core\Checkout\Cart\Delivery\Struct\DeliveryCollection;
use Shopware\Core\Checkout\Cart\Delivery\Struct\DeliveryDate;
use Shopware\Core\Checkout\Cart\Delivery\Struct\DeliveryPositionCollection;
use Shopware\Core\Checkout\Cart\Delivery\Struct\ShippingLocation;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Checkout\Shipping\ShippingMethodEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
class PageSubscriber implements EventSubscriberInterface
{
private $request;
private $config;
private $deliveryRepository;
public function __construct($request, $config, $deliveryRepository)
{
$this->request = $request;
$this->config = $config;
$this->deliveryRepository = $deliveryRepository;
}
public static function getSubscribedEvents()
{
return [
GenericPageLoadedEvent::class => 'onLoad',
BeforeLineItemAddedEvent::class => 'onAdd',
//AfterLineItemAddedEvent::class => 'after'
];
}
/**
* Get sid for api of server
* @param GenericPageLoadedEvent $event
*/
public function onLoad(GenericPageLoadedEvent $event)
{
// only retrieve konfigurator session when logged in
if (!empty($event->getSalesChannelContext()->getCustomer())) {
$customerNumber = $event->getSalesChannelContext()->getCustomer()->getCustomerNumber();
$ch = curl_init("https://konfigurator.seelia.net/serverapi/custom/shop_usersession.php?token=${_ENV['KONFIGURATOR_TOKEN']}&id=${customerNumber}");
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$event->getPage()->assign(['api' => json_decode($result)]);
curl_close($ch);
}
}
/**
* @param BeforeLineItemAddedEvent $event
*/
public function onAdd(BeforeLineItemAddedEvent $event)
{
if ($this->request->getCurrentRequest()->get('new-order')){
$date = date("m/d/Y");
$imgBack = $this->request->getCurrentRequest()->get('img-back');
$imgFront = $this->request->getCurrentRequest()->get('img-front');
$imgGood = $this->request->getCurrentRequest()->get('img-good');
$price = $this->request->getCurrentRequest()->get('new-price');
$configId = $this->request->getCurrentRequest()->get('config-id');
$configDate = $this->request->getCurrentRequest()->get('config-date');
$offerGlb = $this->request->getCurrentRequest()->get('offer-glb');
$offerPdf = $this->request->getCurrentRequest()->get('offer-pdf');
$baoName = $this->request->getCurrentRequest()->get('bao_name');
$doorWidth = $this->request->getCurrentRequest()->get('door_width');
$doorHeight = $this->request->getCurrentRequest()->get('door_height');
$cost = null;
if ($this->request->getCurrentRequest()->get('shippingCost')){
$cost = $this->request->getCurrentRequest()->get('shippingCost');
}
$offerGlb = explode('media', $offerGlb);
$offerGlb = '/media'.$offerGlb[count($offerGlb) - 1];
$lineItem = $event->getLineItem();
$lineItem->setPayloadValue('new_price', $price);
$lineItem->setPayloadValue('added_date', $date);
$lineItem->setPayloadValue('img_front', $imgFront);
$lineItem->setPayloadValue('img_back', $imgBack);
$lineItem->setPayloadValue('img_good', $imgGood);
$lineItem->setPayloadValue('config_id', $configId);
$lineItem->setPayloadValue('config_date', $configDate);
$lineItem->setPayloadValue('offer_glb', $offerGlb);
$lineItem->setPayloadValue('offer_pdf', $offerPdf);
$lineItem->setPayloadValue('bao_name', $baoName);
$lineItem->setPayloadValue('doorWidth', $doorWidth);
$lineItem->setPayloadValue('doorHeight', $doorHeight);
//if ($cost){
// $lineItem->setPayloadValue('cost', $cost);
//}
$cart = $event->getCart();
$referencedId = $lineItem->getReferencedId();
$payload = $lineItem->getPayload();
//dd(count($cart->getLineItems()), $cart->get($referencedId));
if ($cart->get($referencedId) && count($cart->getLineItems()) > 1 && $this->request->getCurrentRequest()->get('new-price')){
$label = $cart->get($referencedId)->getLabel();
$id = Uuid::randomHex();
$newLineItem = new LineItem($id, 'product', $referencedId, 1);
$newLineItem->setRemovable(true);
$newLineItem->setStackable(false);
$payload['productId'] = $referencedId;
$newLineItem->setPayload($payload);
$newLineItem->setLabel($label);
$newPrice = +$this->request->getCurrentRequest()->get('new-price');
$price = new CalculatedPrice($newPrice, $newPrice, $cart->getPrice()->getCalculatedTaxes(), $cart->getPrice()->getTaxRules(), 1 );
$newLineItem->setPrice($price);
$cart->getLineItems()->add($newLineItem);
$cart->getLineItems()->remove($lineItem->getId());
//$cart->addLineItems(new LineItemCollection([$newLineItem]));
}elseif($cart->get($referencedId) && count($cart->getLineItems()) <= 1 && $this->request->getCurrentRequest()->get('new-price')){
$label = $cart->get($referencedId)->getLabel();
$id = Uuid::randomHex();
$newLineItem = new LineItem($id, 'product', $referencedId, 1);
$newLineItem->setRemovable(true);
$newLineItem->setStackable(false);
$payload['productId'] = $referencedId;
$newLineItem->setPayload($payload);
$newLineItem->setLabel($label);
$newPrice = +$this->request->getCurrentRequest()->get('new-price');
$price = new CalculatedPrice($newPrice, $newPrice, $cart->getPrice()->getCalculatedTaxes(), $cart->getPrice()->getTaxRules(), 1 );
$newLineItem->setPrice($price);
$cart->setLineItems(new LineItemCollection([$newLineItem]));
}
}else{
$lineItems = $this->request->getCurrentRequest()->get('lineItems');
if(!empty($lineItems) && isset($lineItems[$event->getLineItem()->getId()]['new_price']))
{
$price = $lineItems[$event->getLineItem()->getId()]['new_price'];
$imgBack = $lineItems[$event->getLineItem()->getId()]['img-back'];
$imgFront = $lineItems[$event->getLineItem()->getId()]['img-front'];
$imgGood = $lineItems[$event->getLineItem()->getId()]['img-good'];
$configId = $lineItems[$event->getLineItem()->getId()]['config_id'];
$offerGlb = $lineItems[$event->getLineItem()->getId()]['offer_glb'];
$offerPdf = $lineItems[$event->getLineItem()->getId()]['offer_pdf'];
if (array_key_exists('bao_name', $lineItems[$event->getLineItem()->getId()])){
$baoName = $lineItems[$event->getLineItem()->getId()]['bao_name'];
}
$cost = null;
if (array_key_exists('shippingCost', $lineItems[$event->getLineItem()->getId()])){
$cost = $this->request->getCurrentRequest()->get('shippingCost');
}
$offerGlb = explode('media', $offerGlb);
$offerGlb = '/media/'.$offerGlb[count($offerGlb) - 1];
$event->getLineItem()->setPayloadValue('new_price', $price);
$event->getLineItem()->setPayloadValue('img_front', $imgFront);
$event->getLineItem()->setPayloadValue('img_back', $imgBack);
$event->getLineItem()->setPayloadValue('img_good', $imgGood);
$event->getLineItem()->setPayloadValue('config_id', $configId);
$event->getLineItem()->setPayloadValue('offer_glb', $offerGlb);
$event->getLineItem()->setPayloadValue('offer_pdf', $offerPdf);
if (isset($baoName)){
$event->getLineItem()->setPayloadValue('bao_name', $baoName);
}
//if ($cost){
//$lineItem->setPayloadValue('cost', $cost);
//}
}
}
if($this->request->getCurrentRequest()->get('shippingCost')){
$cost = $this->request->getCurrentRequest()->get('shippingCost');
//if ($cost){
// $lineItem->setPayloadValue('cost', $cost);
//}
}
}
public function after(AfterLineItemAddedEvent $event)
{
$lineItems = $event->getLineItems();
$cost = [];
foreach ($lineItems as $lineItem){
if(array_key_exists('cost', $lineItem->getPayload())){
$cost[] = +$lineItem->getPayload()['cost'];
}
}
$cost = array_sum($cost);
$key = 'shipping-method-' . $this->config->get('SeeliaApiPlugin')['config']['salesId'];
$shippingMethodPrices = $event->getCart()->getData()->get($key)->getPrices()->first();
$price = new CalculatedPrice($cost, $cost, $event->getCart()->getPrice()->getCalculatedTaxes() ,$event->getCart()->getPrice()->getTaxRules());
dd($price);
//$price = new Price($event->getContext()->getCurrencyId(), $cost, $cost, false);
$shippingMethodPrices->setCurrencyPrice(new PriceCollection([$price]));
dd($event->getCart()->getData()->get($key));
}
}