<?php declare(strict_types=1);
/**
* gb media
* All Rights Reserved.
*
* Unauthorized copying of this file, via any medium is strictly prohibited.
* The content of this file is proprietary and confidential.
*
* @category Shopware
* @package Shopware_Plugins
* @subpackage GbmedEnev
* @copyright Copyright (c) 2019, gb media
* @license proprietary
* @author Giuseppe Bottino
* @link http://www.gb-media.biz
*/
namespace Gbmed\Enev\Subscriber;
use Gbmed\Enev\Core\Content\Enev\EnevDefinition;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CartConverted implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CartConvertedEvent::class => 'processCartConvertedEvent',
];
}
public function processCartConvertedEvent(CartConvertedEvent $event): void
{
$lineItems = $event->getCart()->getLineItems();
$convertedCart = $event->getConvertedCart();
/** @var LineItem $lineItem */
foreach ($lineItems as $lineItem) {
$convertedCartLineItemKey = $this->findLineItemKeyInConvertedCart($convertedCart, $lineItem);
if ($convertedCartLineItemKey === null
|| !array_key_exists($convertedCartLineItemKey, $convertedCart['lineItems'])
) {
continue;
}
if (!$lineItem->hasExtension(EnevDefinition::propertyName())) {
continue;
}
/** @var EntityCollection $data */
$data = $lineItem->getExtension(EnevDefinition::propertyName());
$convertedCart['lineItems'][$convertedCartLineItemKey]['payload']['extensions'][EnevDefinition::propertyName()] = $data;
}
$event->setConvertedCart($convertedCart);
}
private function findLineItemKeyInConvertedCart(array $convertedCart, LineItem $lineItem)
{
foreach ($convertedCart['lineItems'] as $key => $convertedCartLineItem) {
if ($convertedCartLineItem['identifier'] == $lineItem->getId()) {
return $key;
}
}
return null;
}
}