vendor/sylius/sylius/src/Sylius/Component/Core/OrderProcessing/OrderPricesRecalculator.php line 30

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Component\Core\OrderProcessing;
  12. use Sylius\Component\Core\Calculator\ProductVariantPriceCalculatorInterface;
  13. use Sylius\Component\Core\Calculator\ProductVariantPricesCalculatorInterface;
  14. use Sylius\Component\Core\Model\OrderInterface;
  15. use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface;
  16. use Sylius\Component\Order\Processor\OrderProcessorInterface;
  17. use Webmozart\Assert\Assert;
  18. final class OrderPricesRecalculator implements OrderProcessorInterface
  19. {
  20.     public function __construct(private ProductVariantPriceCalculatorInterface|ProductVariantPricesCalculatorInterface $productVariantPriceCalculator)
  21.     {
  22.         if ($this->productVariantPriceCalculator instanceof ProductVariantPriceCalculatorInterface) {
  23.             @trigger_error(
  24.                 sprintf('Passing a "Sylius\Component\Core\Calculator\ProductVariantPriceCalculatorInterface" to "%s" constructor is deprecated since Sylius 1.11 and will be prohibited in 2.0. Use "Sylius\Component\Core\Calculator\ProductVariantPricesCalculatorInterface" instead.'self::class),
  25.                 \E_USER_DEPRECATED,
  26.             );
  27.         }
  28.     }
  29.     public function process(BaseOrderInterface $order): void
  30.     {
  31.         /** @var OrderInterface $order */
  32.         Assert::isInstanceOf($orderOrderInterface::class);
  33.         if (OrderInterface::STATE_CART !== $order->getState()) {
  34.             return;
  35.         }
  36.         $channel $order->getChannel();
  37.         foreach ($order->getItems() as $item) {
  38.             if ($item->isImmutable()) {
  39.                 continue;
  40.             }
  41.             $item->setUnitPrice($this->productVariantPriceCalculator->calculate(
  42.                 $item->getVariant(),
  43.                 ['channel' => $channel],
  44.             ));
  45.             if ($this->productVariantPriceCalculator instanceof ProductVariantPricesCalculatorInterface) {
  46.                 $item->setOriginalUnitPrice($this->productVariantPriceCalculator->calculateOriginal(
  47.                     $item->getVariant(),
  48.                     ['channel' => $channel],
  49.                 ));
  50.             }
  51.         }
  52.     }
  53. }