vendor/shopware/core/Checkout/Cart/Processor.php line 118

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart;
  3. use Shopware\Core\Checkout\Cart\Hook\CartHook;
  4. use Shopware\Core\Checkout\Cart\Price\AmountCalculator;
  5. use Shopware\Core\Checkout\Cart\Transaction\TransactionProcessor;
  6. use Shopware\Core\Framework\Feature;
  7. use Shopware\Core\Framework\Script\Execution\ScriptExecutor;
  8. use Shopware\Core\Profiling\Profiler;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. class Processor
  11. {
  12.     private Validator $validator;
  13.     private AmountCalculator $amountCalculator;
  14.     private TransactionProcessor $transactionProcessor;
  15.     /**
  16.      * @var CartProcessorInterface[]
  17.      */
  18.     private $processors;
  19.     /**
  20.      * @var CartDataCollectorInterface[]
  21.      */
  22.     private $collectors;
  23.     private ScriptExecutor $executor;
  24.     /**
  25.      * @internal
  26.      */
  27.     public function __construct(
  28.         Validator $validator,
  29.         AmountCalculator $amountCalculator,
  30.         TransactionProcessor $transactionProcessor,
  31.         iterable $processors,
  32.         iterable $collectors,
  33.         ScriptExecutor $executor
  34.     ) {
  35.         $this->validator $validator;
  36.         $this->amountCalculator $amountCalculator;
  37.         $this->transactionProcessor $transactionProcessor;
  38.         $this->processors $processors;
  39.         $this->collectors $collectors;
  40.         $this->executor $executor;
  41.     }
  42.     public function process(Cart $originalSalesChannelContext $contextCartBehavior $behavior): Cart
  43.     {
  44.         return Profiler::trace('cart::process', function () use ($original$context$behavior) {
  45.             $cart = new Cart($original->getName(), $original->getToken());
  46.             $cart->setCustomerComment($original->getCustomerComment());
  47.             $cart->setAffiliateCode($original->getAffiliateCode());
  48.             $cart->setCampaignCode($original->getCampaignCode());
  49.             $cart->setBehavior($behavior);
  50.             $cart->addState(...$original->getStates());
  51.             // move data from previous calculation into new cart
  52.             $cart->setData($original->getData());
  53.             $this->runProcessors($original$cart$context$behavior);
  54.             if ($behavior->hookAware()) {
  55.                 $this->executor->execute(new CartHook($cart$context));
  56.             }
  57.             $this->calculateAmount($context$cart);
  58.             $cart->addErrors(
  59.                 ...$this->validator->validate($cart$context)
  60.             );
  61.             $cart->setTransactions(
  62.                 $this->transactionProcessor->process($cart$context)
  63.             );
  64.             $cart->setRuleIds($context->getRuleIds());
  65.             return $cart;
  66.         }, 'cart');
  67.     }
  68.     private function runProcessors(Cart $originalCart $cartSalesChannelContext $contextCartBehavior $behavior): void
  69.     {
  70.         if ($original->getLineItems()->count() <= 0
  71.             && (Feature::isActive('v6.5.0.0') || Feature::isActive('PERFORMANCE_TWEAKS'))
  72.         ) {
  73.             $cart->addErrors(...array_values($original->getErrors()->getPersistent()->getElements()));
  74.             $cart->setExtensions($original->getExtensions());
  75.             return;
  76.         }
  77.         // enrich cart with all required data
  78.         foreach ($this->collectors as $collector) {
  79.             $collector->collect($cart->getData(), $original$context$behavior);
  80.         }
  81.         $cart->addErrors(...array_values($original->getErrors()->getPersistent()->getElements()));
  82.         $cart->setExtensions($original->getExtensions());
  83.         $this->calculateAmount($context$cart);
  84.         // start processing, cart will be filled step by step with line items of original cart
  85.         foreach ($this->processors as $processor) {
  86.             $processor->process($cart->getData(), $original$cart$context$behavior);
  87.             $this->calculateAmount($context$cart);
  88.         }
  89.     }
  90.     private function calculateAmount(SalesChannelContext $contextCart $cart): void
  91.     {
  92.         $amount $this->amountCalculator->calculate(
  93.             $cart->getLineItems()->getPrices(),
  94.             $cart->getDeliveries()->getShippingCosts(),
  95.             $context
  96.         );
  97.         $cart->setPrice($amount);
  98.     }
  99. }