custom/plugins/OrcaOci/src/Subscriber/CheckoutCartSubscriber.php line 48

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 Orcamultimedia\OciPunchout\Struct\OciPunchoutData;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class CheckoutCartSubscriber implements EventSubscriberInterface
  11. {
  12.     public const CONFIG_KEYS = [
  13.         'extcategoryid''leadtime''manufactmat''matgroup''matnr''unit''vendor',
  14.         'customfield1''customfield2''customfield3''customfield4''customfield5'
  15.     ];
  16.     /**
  17.      * @var OciService
  18.      */
  19.     private OciService $ociService;
  20.     /**
  21.      * @var SystemConfigService
  22.      */
  23.     private SystemConfigService $systemConfigService;
  24.     public function __construct(
  25.         OciService          $ociService,
  26.         SystemConfigService $systemConfigService
  27.     )
  28.     {
  29.         $this->ociService $ociService;
  30.         $this->systemConfigService $systemConfigService;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             CheckoutCartPageLoadedEvent::class => 'addOciPunchoutPageData',
  36.         ];
  37.     }
  38.     public function addOciPunchoutPageData(CheckoutCartPageLoadedEvent $event): void
  39.     {
  40.         if (!$this->ociService->isPunchout()) {
  41.             return;
  42.         }
  43.         $page $event->getPage();
  44.         $request $event->getRequest();
  45.         $context $event->getSalesChannelContext();
  46.         $config $this->systemConfigService->get('OciPunchout.config'$context->getSalesChannel()->getId());
  47.         $punchoutData = new OciPunchoutData();
  48.         $oci $request->getSession()->get('oci');
  49.         $lineItems $page->getCart()->getLineItems();
  50.         foreach ($lineItems as $lineItem) {
  51.             $cartDataArray = [];
  52.             foreach (self::CONFIG_KEYS as $key) {
  53.                 $cartDataArray[$key] = $this->getLineItemData($config[$key] ?? null$lineItem);
  54.             }
  55.             $cartDataArray['description'] = $this->getLineItemData('@description' ?? null$lineItem);
  56.             $cartData = new OciCartData();
  57.             $cartData->assign($cartDataArray);
  58.             $lineItem->addExtension(OciCartData::EXTENSION_NAME$cartData);
  59.         }
  60.         $punchoutData->assign([
  61.             'hookUrl' => $oci['hookUrl'],
  62.             'returnTarget' => $oci['returnTarget'],
  63.             'currencyIsoCode' => $page->getHeader()->getActiveCurrency()->getIsoCode(),
  64.             'lineItems' => $page->getCart()->getLineItems()
  65.         ]);
  66.         $page->addExtension(OciPunchoutData::EXTENSION_NAME$punchoutData);
  67.     }
  68.     private function getLineItemData($config$lineItem)
  69.     {
  70.         if (!$config || strpos($config'@') === false) {
  71.             return $config;
  72.         }
  73.         $config str_replace('@'''$config);
  74.         if (
  75.             ($lineItemData $lineItem->getVars()[$config] ?? null)
  76.             || ($lineItemData $lineItem->getPayloadValue($config) ?? null)
  77.             || ($lineItem->hasExtension(OciCartData::EXTENSION_NAME)
  78.                 && $lineItemData $lineItem->getExtension(OciCartData::EXTENSION_NAME)->getVars()[$config] ?? null
  79.             )
  80.         ) {
  81.             return $lineItemData;
  82.         }
  83.         return null;
  84.     }
  85. }