<?php
declare(strict_types=1);
namespace Orcamultimedia\OciPunchout\Subscriber;
use Orcamultimedia\OciPunchout\Checkout\Cart\PunchoutLineItemFactory;
use Orcamultimedia\OciPunchout\Service\OciService;
use Orcamultimedia\OciPunchout\Struct\OciCartData;
use Shopware\Core\Checkout\Cart\AbstractCartPersister;
use Shopware\Core\Checkout\Cart\CartCalculator;
use Shopware\Core\Checkout\Cart\Event\CartChangedEvent;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class CartChangedSubscriber implements EventSubscriberInterface
{
/**
* @var CartCalculator
*/
private CartCalculator $cartCalculator;
/**
* @var AbstractCartPersister
*/
private AbstractCartPersister $cartPersister;
private SystemConfigService $systemConfigService;
private OciService $ociService;
private TranslatorInterface $translator;
public function __construct(
CartCalculator $cartCalculator,
AbstractCartPersister $cartPersister,
SystemConfigService $systemConfigService,
OciService $ociService,
TranslatorInterface $translator
) {
$this->cartCalculator = $cartCalculator;
$this->cartPersister = $cartPersister;
$this->systemConfigService = $systemConfigService;
$this->ociService = $ociService;
$this->translator = $translator;
}
public static function getSubscribedEvents(): array
{
return [
CartChangedEvent::class => 'addShippingCostsItem'
];
}
public function addShippingCostsItem(CartChangedEvent $event): void
{
$cart = $event->getCart();
$context = $event->getContext();
if (
!$this->ociService->isPunchout()
|| !$this->systemConfigService->get(
'OciPunchout.config.addShippingCostsAtPunchout',
$context->getSalesChannel()->getId()
)
|| $cart->getLineItems()->count() === 0
) {
return;
}
if ($cart->has('shipping-costs')) {
$cart->remove('shipping-costs');
}
if($cart->getDeliveries()->getShippingCosts()->sum()->getTotalPrice() > 0) {
$extensionData = new OciCartData();
$extensionData->assign([
'matgroup' => $this->systemConfigService->get(
'OciPunchout.config.shippingCostsMatgroup',
$context->getSalesChannel()->getId()
),
'unit' => 'PCE'
]);
// Adding leadtime
if ($this->systemConfigService->get('OciPunchout.config.isSubmitMaximumLeadTimeForShippingCosts',
$context->getSalesChannel()->getId())) {
$extensionData->assign([
'leadtime' => $this->systemConfigService->get(
'OciPunchout.config.maximumLeadTime',
$context->getSalesChannel()->getId()
)
]);
}
// Adding extschematype
if ($this->systemConfigService->get('OciPunchout.config.isSubmitShippingCostsExtschematype',
$context->getSalesChannel()->getId())) {
$extensionData->assign([
'extschematype' => $this->systemConfigService->get(
'OciPunchout.config.extschematype',
$context->getSalesChannel()->getId()
)
]);
}
// Adding extcategoryid
if ($this->systemConfigService->get('OciPunchout.config.shippingCostsExtcategoryid',
$context->getSalesChannel()->getId())) {
$extensionData->assign([
'extcategoryid' => $this->systemConfigService->get(
'OciPunchout.config.extcategoryid',
$context->getSalesChannel()->getId()
)
]);
}
// Adding Customfield
if (strcasecmp($this->systemConfigService->get('OciPunchout.config.isSubmitShippingCostsCustomfield',
$context->getSalesChannel()->getId()), 'default') !== 0) {
$extensionData->assign([
'extcategoryid' => $this->systemConfigService->get(
'OciPunchout.config.' . $this->systemConfigService->get('OciPunchout.config.isSubmitShippingCostsCustomfield',
$context->getSalesChannel()->getId()),
$context->getSalesChannel()->getId()
)
]);
}
$lineItem = (new LineItem('shipping-costs', PunchoutLineItemFactory::TYPE, 'shipping-costs', 1))
->setLabel($this->translator->trans('orca-ocipunchout.shippingCosts', ['%type%' =>
$context->getShippingMethod()->getName()]))
->setRemovable(true)
->setPayloadValue('productNumber', 'shipping-costs')
->setPrice(
new CalculatedPrice(
$cart->getDeliveries()->getShippingCosts()->sum()->getUnitPrice(),
$cart->getDeliveries()->getShippingCosts()->sum()->getTotalPrice(),
new CalculatedTaxCollection(),
new TaxRuleCollection()
)
);
$alternativeCaptionShippingCosts = $this->systemConfigService->get(
'OciPunchout.config.alternativeCaptionOfShippingCosts',
$context->getSalesChannel()->getId()
);
if (strlen($alternativeCaptionShippingCosts) > 0) {
$alternativeCaptionShippingCosts = trim($alternativeCaptionShippingCosts);
$lineItem->setLabel($alternativeCaptionShippingCosts);
}
$lineItem->addExtension(OciCartData::EXTENSION_NAME, $extensionData);
$cart->add($lineItem);
}
$cart->markModified();
$cart = $this->cartCalculator->calculate($cart, $context);
$this->cartPersister->save($cart, $context);
}
}