<?php declare(strict_types=1);
namespace Xioni\Xconfig\Core\Checkout\Cart\Subscriber;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemRemovedEvent;
use Shopware\Core\Checkout\Cart\Exception\LineItemNotFoundException;
use Shopware\Core\Checkout\Cart\Exception\LineItemNotRemovableException;
use Shopware\Core\Checkout\Cart\Exception\PayloadKeyNotFoundException;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class CheckoutCartSubscriber implements EventSubscriberInterface
{
private $requestStack;
public function __construct(RequestStack $requestStack) {
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents()
{
return [
BeforeLineItemAddedEvent::class => 'onLineItemAdded'
];
}
public function onLineItemAdded(BeforeLineItemAddedEvent $event): void
{
$lineItem = $event->getLineItem();
if($this->requestStack->getCurrentRequest()->get('lineItems') != null) {
foreach ($this->requestStack->getCurrentRequest()->get('lineItems') as $key => $item) {
if ($lineItem->getReferencedId() == $key) {
if (isset($item['xconfig']) && $item['xconfig'] == "1" ) {
$lineItem->setPayloadValue('xconfig', $item['xconfig']);
if (isset($item['xconfigDesc'])) {
$lineItem->setPayloadValue('xconfigDesc', $item['xconfigDesc']);
}
if (isset($item['xGroupIds']) && isset($item['xGroupValues'])) {
$lineItem->setPayloadValue('xGroupIds', $item['xGroupIds']);
$lineItem->setPayloadValue('xGroupValues', $item['xGroupValues']);
$id = 0;
$productNumber = $item['xProductNumber'];
$optionIds = explode(",", $item['xGroupIds']);
$valueIds = explode(",", $item['xGroupValues']);
$configuration = $this->getConfiguration($id, $productNumber, $optionIds, $valueIds);
$xPrice = $configuration->variant_product_price1->raw;
$xProductNumber = $configuration->variant_product_productnumber;
$xProductDescription = $configuration->config_product_description;
$xEan = $configuration->variant_product_ean;
$lineItem->setPayloadValue('xPrice', $xPrice);
$lineItem->setPayloadValue('xProductNumber', $xProductNumber);
$lineItem->setPayloadValue('xEan', $xEan);
$lineItem->setPayloadValue('xProductDescription', $xProductDescription);
$lineItem->setPayloadValue('xMedia', $item['xconfigMedia']);
}
}
if (isset($item['xconfig_accessory']) && $item['xconfig_accessory'] == "1" ) {
$lineItem->setPayloadValue('xconfig_accessory', $item['xconfig_accessory']);
$id = 0;
$lineItem->setPayloadValue('variant_relation', $item['xconfig_accessory_variant_relation']);
}
}
}
}
}
public function getConfiguration($id, $productNumber, $optionIds, $valueIds)
{
$url = 'https://xconfig003.com/mvc_structure2/get_configuration_poller.php';
$data = array('id' => $id, 'productnumber' => $productNumber, 'option_ids' => $optionIds, 'value_ids' => $valueIds);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$configuration = file_get_contents($url, false, $context);
$configuration = json_decode($configuration);
return $configuration;
}
}