<?php
declare(strict_types=1);
namespace Wbfk\Offer\Subscriber;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Wbfk\Offer\Core\Checkout\Cart\Custom\CartMode;
class AddressListingPageLoadedEventSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly CartService $cart)
{
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
AddressListingPageLoadedEvent::class => 'onAddressListingPageLoaded',
];
}
/**
* User can edit the address from `checkout/cart` and checkout/offer pages.
* By default, shopware redirects user to `checkout/cart` page after editing the address.
* To redirect the user to the same page from where the user has initiated the address edit, we need to set the redirectTo parameter.
*
* @param AddressListingPageLoadedEvent $event
* @return void
*/
public function onAddressListingPageLoaded(AddressListingPageLoadedEvent $event): void
{
$cart = $this->cart->getCart($event->getSalesChannelContext()->getToken(), $event->getSalesChannelContext());
/** @var ?CartMode $cartMode */
$cartMode = $cart->getExtension('cartMode');
$currentCartMode = $cartMode?->getSelectedCartMode() ?? 'cart';
$redirectTo = $currentCartMode === 'cart' ? 'frontend.checkout.cart.page' : 'frontend.wbfk-offer.checkout.offer.page';
$event->getPage()->assign(['redirectTo' => $redirectTo]);
}
}