<?php
declare(strict_types=1);
namespace Meteor\PromotionGift\Subscribers;
use Meteor\PromotionGift\Core\Checkout\Promotion\Cart\GiftProcessor;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class RemovePromoWrapperFromOrder implements EventSubscriberInterface
{
public function __invoke(CartConvertedEvent $event): void
{
$convertedCart = $event->getConvertedCart();
if (!array_key_exists('lineItems', $convertedCart)) {
return;
}
$convertedCart['lineItems'] = $this->removePromotionGiftWrappersFromLineItems(
$convertedCart['lineItems']
);
$convertedCart['lineItems'] = $this->removeParentReferenceFromGiftProductLineItems(
$convertedCart['lineItems']
);
$event->setConvertedCart($convertedCart);
}
public static function getSubscribedEvents(): array
{
return [
CartConvertedEvent::class => '__invoke',
];
}
private function removePromotionGiftWrappersFromLineItems(array $lineItems): array
{
return array_filter($lineItems, function (array $lineItem) {
$lineItemType = $lineItem['payload']['line_item_type'] ?? null;
if (null === $lineItemType) {
return true;
}
return GiftProcessor::PROMOTION_GIFT_WRAPPER_TYPE !== $lineItemType;
});
}
private function removeParentReferenceFromGiftProductLineItems(array $lineItems): array
{
return array_map(function (array $lineItem) {
$lineItemType = $lineItem['payload']['line_item_type'] ?? null;
if (null === $lineItemType) {
return $lineItem;
}
if (GiftProcessor::PROMOTION_GIFT_PRODUCT_TYPE !== $lineItemType) {
return $lineItem;
}
$lineItem['parentId'] = null;
return $lineItem;
}, $lineItems);
}
}