<?php declare(strict_types=1);
/**
* Shopware
* Copyright © 2021
*
* @category Shopware
* @package SwpSampleOrderSix
* @subpackage Frontend.php
*
* @copyright 2021 Iguana-Labs GmbH
* @author Module Factory <info at module-factory.com>
* @license https://www.module-factory.com/eula
*/
namespace Swp\SampleOrderSix\Subscriber;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemQuantityChangedEvent;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Swp\SampleOrderSix\Core\SampleOrderSix\Price\SamplePriceDefinitionBuilderInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Frontend implements EventSubscriberInterface
{
/** @var SystemConfigService */
private $systemConfigService;
/**
* Frontend constructor.
* @param SystemConfigService $systemConfigService
*/
public function __construct(
SystemConfigService $systemConfigService
) {
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
BeforeLineItemQuantityChangedEvent::class => 'onBeforeLineItemQuantityChanged'
];
}
/**
* @param BeforeLineItemQuantityChangedEvent $event
* @return mixed
*/
public function onBeforeLineItemQuantityChanged(BeforeLineItemQuantityChangedEvent $event)
{
if ($this->isCannelActive($event->getSalesChannelContext()) !== true) {
return;
}
if (strpos($event->getLineItem()->getId(), "sample") !== false) {
// !! Wichtig um wieder den richtigen Type zu setzen nach Quantity setzen
$event->getLineItem()->setType('swpsampleorder');
}
}
/**
* @param SalesChannelContext $event
* @return array|mixed|null
*/
private function isCannelActive(SalesChannelContext $event)
{
return $this->systemConfigService->get(
'SwpSampleOrderSix.config.channelActive',
$event->getSalesChannel()->getId()
);
}
}