custom/plugins/OrcaOci/src/Subscriber/LineItemAddedSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Orcamultimedia\OciPunchout\Subscriber;
  4. use Orcamultimedia\OciPunchout\Service\OciService;
  5. use Orcamultimedia\OciPunchout\Struct\OciCartData;
  6. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class LineItemAddedSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var SalesChannelRepository
  15.      */
  16.     private SalesChannelRepository $productRepository;
  17.     /**
  18.      * @var OciService
  19.      */
  20.     private OciService $ociService;
  21.     /**
  22.      * @var SystemConfigService
  23.      */
  24.     private SystemConfigService $systemConfigService;
  25.     public function __construct(
  26.         SalesChannelRepository $productRepository,
  27.         OciService             $ociService,
  28.         SystemConfigService $systemConfigService
  29.     )
  30.     {
  31.         $this->productRepository $productRepository;
  32.         $this->ociService $ociService;
  33.         $this->systemConfigService $systemConfigService;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             BeforeLineItemAddedEvent::class => 'addPayloadValues',
  39.         ];
  40.     }
  41.     public function addPayloadValues(BeforeLineItemAddedEvent $event): void
  42.     {
  43.         if (!$this->ociService->isPunchout()) {
  44.             return;
  45.         }
  46.         $lineItem $event->getLineItem();
  47.         $context $event->getSalesChannelContext();
  48.         $config $this->systemConfigService->get('OciPunchout.config'$context->getSalesChannel()->getId());
  49.         $product $this->productRepository->search(new Criteria([$lineItem->getReferencedId()]), $context)->first();
  50.         $extensionData = new OciCartData();
  51.         if ($product) {
  52.             $cartDataArray $this->getCartDataArray($config$product);
  53.             $extensionData->assign($cartDataArray);
  54.         }
  55.         $lineItem->addExtension(OciCartData::EXTENSION_NAME$extensionData);
  56.     }
  57.     private function getCartDataArray(array $config$product): array
  58.     {
  59.         $cartDataArray = [];
  60.         foreach (CheckoutCartSubscriber::CONFIG_KEYS as $key) {
  61.             $configValue $config[$key] ?? '';
  62.             if (!$configValue || strpos($configValue'@') === false) {
  63.                 continue;
  64.             }
  65.             $configValue str_replace('@'''$configValue);
  66.             if(
  67.                 ($data $product->getCustomFields()[$configValue] ?? null)
  68.                 || ($data $product->getVars()[$configValue] ?? null)
  69.             ) {
  70.                 $cartDataArray[$configValue] = $data;
  71.             }
  72.         }
  73.         $cartDataArray['description'] = $product->getDescription();
  74.         return $cartDataArray;
  75.     }
  76. }