<?php
declare(strict_types=1);
namespace Wbfk\Offer\Core\Checkout\SalesChannel;
use Exception;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Wbfk\Offer\Core\Offer\Storefront\OfferService;
class CartServiceDecorator extends CartService
{
public function __construct(
private readonly OfferService $offerService,
private readonly CartService $decorated
) {
}
public function getDecorated(): CartService
{
return $this->decorated;
}
public function setCart(Cart $cart): void
{
$this->decorated->setCart($cart);
}
public function createNew(string $token, string $name = self::SALES_CHANNEL): Cart
{
return $this->decorated->createNew($token, $name);
}
public function getCart(string $token, SalesChannelContext $context, string $name = self::SALES_CHANNEL, bool $caching = true): Cart
{
return $this->decorated->getCart($token, $context, $name, $caching);
}
public function add(Cart $cart, $items, SalesChannelContext $context): Cart
{
return $this->decorated->add($cart, $items, $context);
}
/**
* @throws Exception
*/
public function changeQuantity(Cart $cart, string $identifier, int $quantity, SalesChannelContext $context): Cart
{
$offerItemId = $cart->getLineItems()->get($identifier)->getPayloadValue('offer-item')['id'] ?? false;
$offerId = $cart->getLineItems()->get($identifier)->getPayloadValue('offer-item')['offerId'] ?? false;
if (!$offerItemId || !$offerId) {
return $this->decorated->changeQuantity($cart, $identifier, $quantity, $context);
}
$offer = $this->offerService->getOfferById($offerId, $context->getContext());
$this->offerService->changeLineItemQuantity($offer, $offerItemId, $quantity, $context->getContext());
return $this->decorated->changeQuantity($cart, $identifier, $quantity, $context);
}
public function remove(Cart $cart, string $identifier, SalesChannelContext $context): Cart
{
return $this->decorated->remove($cart, $identifier, $context);
}
public function order(Cart $cart, SalesChannelContext $context, RequestDataBag $data): string
{
return $this->decorated->order($cart, $context, $data);
}
public function recalculate(Cart $cart, SalesChannelContext $context): Cart
{
return $this->decorated->recalculate($cart, $context);
}
public function deleteCart(SalesChannelContext $context): void
{
$this->decorated->deleteCart($context);
}
public function reset(): void
{
$this->decorated->reset();
}
}