<?php declare(strict_types=1);
namespace Zeobv\BundleProducts\Storefront\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
/**
* Class OrderDetailPageSubscriber
*
* @package Zeobv\BundleProducts\Storefront\Subscriber
*/
class OrderDetailPageSubscriber implements EventSubscriberInterface
{
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
AccountOrderPageLoadedEvent::class => 'onOrderPageLoaded',
];
}
/**
* @param AccountOrderPageLoadedEvent $event
*/
public function onOrderPageLoaded(AccountOrderPageLoadedEvent $event): void
{
$orders = $event->getPage()->getOrders();
/** @var OrderEntity $order */
foreach ($orders as $order) {
# Order by position, but try to keep children under bundle products
$order->getLineItems()->sort(function (OrderLineItemEntity $a, OrderLineItemEntity $b) {
return $a->getPosition() > $b->getPosition() || $a->getParentId() !== null;
});
# place promotions and credit line at the bottom
$order->getLineItems()->sort(function (OrderLineItemEntity $a, OrderLineItemEntity $b) {
return $a->getType() === LineItem::PROMOTION_LINE_ITEM_TYPE || $a->getType() === LineItem::CREDIT_LINE_ITEM_TYPE;
});
}
}
}