<?php declare(strict_types=1);
namespace FourtwosixThemeExtension\Subscriber;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CheckoutSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CheckoutCartPageLoadedEvent::class => "onCartPageLoaded",
CheckoutConfirmPageLoadedEvent::class => "onConfirmPageLoaded",
CheckoutFinishPageLoadedEvent::class => "onFinishPageLoaded",
CheckoutRegisterPageLoadedEvent::class => "onCheckoutRegisterPageLoaded",
OffcanvasCartPageLoadedEvent::class => "onOffcanvasCartPageLoaded",
];
}
public function onCartPageLoaded(CheckoutCartPageLoadedEvent $event): void
{
$this->addExtensionToCart($event->getPage()->getCart());
}
public function onConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
{
$this->addExtensionToCart($event->getPage()->getCart());
}
public function onFinishPageLoaded(CheckoutFinishPageLoadedEvent $event): void
{
$this->addExtensionToCart($event->getPage()->getOrder());
}
public function onCheckoutRegisterPageLoaded(CheckoutRegisterPageLoadedEvent $event): void
{
$this->addExtensionToCart($event->getPage()->getCart());
}
public function onOffcanvasCartPageLoaded(OffcanvasCartPageLoadedEvent $event): void
{
$this->addExtensionToCart($event->getPage()->getCart());
}
private function addExtensionToCart(Cart|OrderEntity $cart): void
{
$promotionCompositionByLineItem = [];
/** @var LineItem $lineItem */
foreach ($cart->getLineItems() as $lineItem) {
if ($lineItem->getType() === LineItem::PROMOTION_LINE_ITEM_TYPE) {
$compositions = $lineItem->getPayload()["composition"] ?? [];
$promotionCodeType = $lineItem->getPayload()["promotionCodeType"] ?? [];
$discountScope = $lineItem->getPayload()["discountScope"] ?? [];
$extensionArrayStruct = [];
foreach ($compositions as $composition) {
if (!isset($promotionCompositionByLineItem[$composition["id"]])) {
$promotionCompositionByLineItem[$composition["id"]] = 0;
}
$promotionCompositionByLineItem[$composition["id"]] += $composition["discount"];
}
if($discountScope === "delivery"){
$deliveries = $cart->getDeliveries();
$deliveryDiscount = 0;
foreach ($deliveries as $delivery) {
$deliveryShippingPrice = $delivery->getShippingCosts()->getTotalPrice();
if( $deliveryShippingPrice < 0){
$deliveryDiscount += $deliveryShippingPrice;
}
}
// @fourtwosix-edit calcualated deliveryDiscount
$extensionArrayStruct["deliveryDiscount"] = $deliveryDiscount;
}
$extensionArrayStruct["isApplied"] = !empty($compositions) && $promotionCodeType === 'global';
$lineItem->addExtension("fourtwosixThemeExtension", new ArrayStruct($extensionArrayStruct));
}
}
foreach ($cart->getLineItems() as $lineItem) {
if ($lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
if ($lineItem instanceof OrderLineItemEntity) {
$lineItem->addExtension("fourtwosixThemeExtension", new ArrayStruct([
"appliedDiscount" => $promotionCompositionByLineItem[$lineItem->getProductId()] ?? 0,
]));
} else {
$lineItem->addExtension("fourtwosixThemeExtension", new ArrayStruct([
"appliedDiscount" => $promotionCompositionByLineItem[$lineItem->getId()] ?? 0,
]));
}
}
}
}
}