custom/static-plugins/CioFormBuilder/src/Subscriber/PodSurchargeCartProcessedSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CioFormBuilder\Subscriber;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Checkout\Cart\Event\CartProcessedEvent;
  6. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  7. use Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator;
  8. use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class PodSurchargeCartProcessedSubscriber implements EventSubscriberInterface
  11. {
  12.     private QuantityPriceCalculator $calculator;
  13.     private LoggerInterface $logger;
  14.     public function __construct(
  15.         QuantityPriceCalculator $calculator,
  16.         LoggerInterface $logger
  17.     ) {
  18.         $this->calculator $calculator;
  19.         $this->logger $logger;
  20.         $this->logger->info('[CIO-AI-Driven] PodSurchargeCartProcessedSubscriber.construct');
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             CartProcessedEvent::class => 'onCartProcessed',
  26.         ];
  27.     }
  28.     public function onCartProcessed(CartProcessedEvent $event): void
  29.     {
  30.         $cart $event->getCart();
  31.         $this->logger->info('[CIO-AI-Driven] PodSurchargeCartProcessedSubscriber.start', [
  32.             'cartToken' => $cart->getToken(),
  33.             'lineItemCount' => $cart->getLineItems()->count(),
  34.             'ids' => $cart->getLineItems()->getKeys(),
  35.         ]);
  36.         foreach ($cart->getLineItems() as $lineItem) {
  37.             if (!$lineItem instanceof LineItem) {
  38.                 continue;
  39.             }
  40.             if ($lineItem->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) {
  41.                 continue;
  42.             }
  43.             $payload $lineItem->getPayload() ?? [];
  44.             $cf $payload['customFields'] ?? [];
  45.             $isPod $cf['custom_pod_products_isPodProduct'] ?? null;
  46.             $surcharges $cf['cioFormSurcharges'] ?? [];
  47.             $formData $cf['cioFormData'] ?? [];
  48.             $tableData $cf['cioTableFormData'] ?? [];
  49.             if ($isPod !== true || empty($surcharges)) {
  50.                 continue;
  51.             }
  52.             if (is_array($tableData)) {
  53.                 foreach ($tableData as $row) {
  54.                     if (is_array($row)) {
  55.                         foreach ($row as $field) {
  56.                             $formData[] = $field;
  57.                         }
  58.                     }
  59.                 }
  60.             }
  61.             $matched = [];
  62.             $positionSurcharge 0.0;
  63.             $perUnitSurcharge 0.0;
  64.             $surchargeMap = [];
  65.             foreach ($surcharges as $s) {
  66.                 if (!empty($s['id'])) {
  67.                     $surchargeMap[$s['id']] = $s;
  68.                 }
  69.             }
  70.             foreach ($formData as $fieldData) {
  71.                 $fieldId $fieldData['id'] ?? null;
  72.                 if (!$fieldId || !isset($surchargeMap[$fieldId])) {
  73.                     continue;
  74.                 }
  75.                 $cfg $surchargeMap[$fieldId];
  76.                 $amount = (float)($cfg['amount'] ?? 0.0);
  77.                 if ($amount <= 0) {
  78.                     continue;
  79.                 }
  80.                 $mode $cfg['mode'] ?? 'position';
  81.                 $value $fieldData['value'] ?? null;
  82.                 $type $fieldData['type'] ?? '';
  83.                 $hasValue false;
  84.                 if (in_array($type, ['text''multiline_text''date'], true)) {
  85.                     $hasValue is_string($value) && trim($value) !== '';
  86.                 } elseif ($type === 'select') {
  87.                     $hasValue is_string($value) && $value !== '';
  88.                 } elseif ($type === 'checkbox') {
  89.                     $hasValue = (bool)$value;
  90.                 }
  91.                 if (!$hasValue) {
  92.                     continue;
  93.                 }
  94.                 $matched[] = [
  95.                     'id' => $fieldId,
  96.                     'type' => $type,
  97.                     'mode' => $mode,
  98.                     'amount' => $amount,
  99.                     'value' => $value,
  100.                 ];
  101.                 if ($mode === 'per_unit') {
  102.                     $perUnitSurcharge += $amount;
  103.                 } else {
  104.                     $positionSurcharge += $amount;
  105.                 }
  106.             }
  107.             if (empty($matched)) {
  108.                 continue;
  109.             }
  110.             $price $lineItem->getPrice();
  111.             if ($price === null) {
  112.                 continue;
  113.             }
  114.             $quantity max(1$lineItem->getQuantity());
  115.             $unitPrice $price->getUnitPrice();
  116.             $perUnitPosition $positionSurcharge ? ($positionSurcharge $quantity) : 0.0;
  117.             $unitWith $unitPrice $perUnitSurcharge $perUnitPosition;
  118.             $definition = new QuantityPriceDefinition(
  119.                 $unitWith,
  120.                 $price->getTaxRules(),
  121.                 $quantity,
  122.                 $price->getReferencePrice(),
  123.                 $price->getListPrice(),
  124.                 $price->getRegulationPrice()
  125.             );
  126.             $calculated $this->calculator->calculate($definition$event->getSalesChannelContext());
  127.             $lineItem->setPriceDefinition($definition);
  128.             $lineItem->setPrice($calculated);
  129.             $this->logger->info('[CIO-AI-Driven] PodSurchargeCartProcessedSubscriber.apply', [
  130.                 'lineItemId' => $lineItem->getId(),
  131.                 'referencedId' => $lineItem->getReferencedId(),
  132.                 'quantity' => $quantity,
  133.                 'baseUnitPrice' => $unitPrice,
  134.                 'positionSurcharge' => $positionSurcharge,
  135.                 'perUnitSurcharge' => $perUnitSurcharge,
  136.                 'perUnitPositionPart' => $perUnitPosition,
  137.                 'unitPriceWithSurcharge' => $unitWith,
  138.                 'totalPrice' => $calculated->getTotalPrice(),
  139.                 'matchedFields' => $matched,
  140.             ]);
  141.         }
  142.     }
  143. }