<?php declare(strict_types=1);
namespace DreiscSet\Subscriber\Events;
use DreiscSet\Core\Cart\LineItemFactoryHandler\DreiscSetItemLineItemFactory;
use DreiscSet\Core\Cart\LineItemFactoryHandler\DreiscSetLineItemFactory;
use DreiscSet\Core\SetOrder\SetOrderConfiguration\Struct\SetOrderConfigurationItemStruct;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Shopware\Core\Checkout\Cart\Order\OrderConvertedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CartConvertedEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
CartConvertedEvent::class => 'onCartConvertedEvent',
OrderConvertedEvent::class => 'onOrderConvertedEvent',
];
}
/**
* Is called, when the cart will convert to an order
*
* @param CartConvertedEvent $event
* @return void
*/
public function onCartConvertedEvent(CartConvertedEvent $event)
{
}
/**
* Is called, when the order will convert back to a cart (recalculation)
*
* @param OrderConvertedEvent $event
* @return void
*/
public function onOrderConvertedEvent(OrderConvertedEvent $event)
{
}
//
// /**
// * Is called, when the cart will convert to an order
// *
// * @param CartConvertedEvent $event
// * @return void
// */
// public function onCartConvertedEvent(CartConvertedEvent $event)
// {
// return;
//
// /** @var array{lineItems:string} $convertedCart */
// $convertedCart = $event->getConvertedCart();
//
// /** Fetch all line item ids of sets */
// $dreiscSetLineItemIds = $this->fetchDreiscSetLineItemIds($convertedCart['lineItems']);
//
// /** Abort, if there is no set in the cart */
// if(empty($dreiscSetLineItemIds)) {
// return;
// }
//
// /** Iterate all sets and clean up the cart info */
// foreach($dreiscSetLineItemIds as $dreiscSetLineItemId) {
// $convertedCart['lineItems'] = $this->cleanUpSetChildren($dreiscSetLineItemId, $convertedCart['lineItems']);
// }
//
// /** Update the converted cart */
// $event->setConvertedCart($convertedCart);
// }
//
// /**
// * Is called, when the order will convert back to a cart (recalculation)
// *
// * @param OrderConvertedEvent $event
// * @return void
// */
// public function onOrderConvertedEvent(OrderConvertedEvent $event)
// {
// return;
// $convertedCart = $event->getConvertedCart();
//
// /** Iterate all items to find the set items */
// /** @var LineItem $lineItem */
// foreach($convertedCart->getLineItems() as $lineItem) {
// /** Abort, if it´s not a set */
// if ($lineItem->getType() !== DreiscSetLineItemFactory::LINE_ITEM_TYPE) {
// continue;
// }
//
// $lineItemPayload = $lineItem->getPayload();
// $lineItemPayload['originType'] = $lineItem->getType();
// $lineItem->setPayload($lineItemPayload);
//
// $lineItem->setType(LineItem::PRODUCT_LINE_ITEM_TYPE);
// }
//
// /** Update the converted cart */
// $event->setConvertedCart($convertedCart);
// }
//
// /**
// * @param array $lineItems
// * @return array
// */
// private function fetchDreiscSetLineItemIds(array $lineItems): array
// {
// $dreiscSetLineItemIds = [];
//
// /** @var array{id: string, type: string} $lineItem */
// foreach($lineItems as $lineItem) {
// if (DreiscSetLineItemFactory::LINE_ITEM_TYPE !== $lineItem['type']) {
// continue;
// }
//
// $dreiscSetLineItemIds[] = $lineItem['id'];
// }
//
// return $dreiscSetLineItemIds;
// }
//
// /**
// * @param $dreiscSetLineItemId
// * @param $lineItems
// * @return array
// */
// private function cleanUpSetChildren($dreiscSetLineItemId, $lineItems): array
// {
// $filteredLineItems = [];
// $baseProduct = [];
//
// /** Iterate the items to find the children of the set */
// /** @var array{type: string, parentId: string, identifier: string, referencedId: string, productId: string, payload: array} $lineItem */
// foreach($lineItems as $lineItem)
// {
// if (empty($lineItem['parentId']) || $lineItem['parentId'] !== $dreiscSetLineItemId) {
// /** No parent id is set or parent id does not match */
// /** So it is not part of the set */
// $filteredLineItems[] = $lineItem;
//
// continue;
// }
//
// if (LineItem::PRODUCT_LINE_ITEM_TYPE === $lineItem['type']) {
// /** Backup the base product */
// $baseProduct = $lineItem;
//
// /** We found the base product of the set, which we will remove */
// /** So we abort at this point */
// continue;
// }
//
// if (in_array($lineItem['type'], [SetOrderConfigurationItemStruct::ITEM_TYPE__MAIN_PRODUCT, DreiscSetItemLineItemFactory::LINE_ITEM_TYPE], true))
// {
// /** We found the set main product or a child of the set, which we will convert to a product line item */
// /** Abort, if no product is set in the payload [@see DreiscSetLineItemService::addDreiscSetItems()] */
//
// if (!empty($lineItem['payload']['productId'])) {
// /** Backup the origin data */
// $lineItem['payload']['originType'] = $lineItem['type'];
// $lineItem['payload']['originIdentifier'] = $lineItem['identifier'];
// $lineItem['payload']['originReferencedId'] = $lineItem['referencedId'];
//
// /** Set the type from "mainProduct" or "dreisc-set-item" to "product" */
// $lineItem['type'] = LineItem::PRODUCT_LINE_ITEM_TYPE;
//
// /** Set the product id as in the shopware default */
// $lineItem['identifier'] = $lineItem['payload']['productId'];
// $lineItem['referencedId'] = $lineItem['payload']['productId'];
// $lineItem['productId'] = $lineItem['payload']['productId'];
// }
// }
//
// $filteredLineItems[] = $lineItem;
// }
//
// /** Put the deleted base product and the converted flag to the set line item */
// foreach($filteredLineItems as &$filteredLineItem) {
// if ($dreiscSetLineItemId === $filteredLineItem['id']) {
// $filteredLineItem['payload']['converted'] = true;
// $filteredLineItem['payload']['baseProduct'] = $baseProduct;
// }
// }
//
// return $filteredLineItems;
// }
}