vendor/shopware/core/Checkout/Cart/LineItem/LineItemQuantitySplitter.php line 14

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart\LineItem;
  3. use Shopware\Core\Checkout\Cart\Exception\InvalidQuantityException;
  4. use Shopware\Core\Checkout\Cart\Exception\LineItemNotStackableException;
  5. use Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator;
  6. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  7. use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. #[Package('checkout')]
  11. class LineItemQuantitySplitter
  12. {
  13.     /**
  14.      * @var QuantityPriceCalculator
  15.      */
  16.     private $quantityPriceCalculator;
  17.     /**
  18.      * @internal
  19.      */
  20.     public function __construct(QuantityPriceCalculator $quantityPriceCalculator)
  21.     {
  22.         $this->quantityPriceCalculator $quantityPriceCalculator;
  23.     }
  24.     /**
  25.      * Gets a new line item with only the provided quantity amount
  26.      * along a ready-to-use calculated price.
  27.      *
  28.      * @throws InvalidQuantityException
  29.      * @throws LineItemNotStackableException
  30.      */
  31.     public function split(LineItem $itemint $quantitySalesChannelContext $context): LineItem
  32.     {
  33.         if ($item->getQuantity() === $quantity) {
  34.             return clone $item;
  35.         }
  36.         // clone the original line item
  37.         $tmpItem = clone $item;
  38.         // use calculated item price
  39.         /** @var CalculatedPrice $lineItemPrice */
  40.         $lineItemPrice $tmpItem->getPrice();
  41.         // use calculated item price
  42.         $unitPrice $lineItemPrice->getUnitPrice();
  43.         $taxRules $lineItemPrice->getTaxRules();
  44.         // change the quantity to 1 single item
  45.         $tmpItem->setQuantity($quantity);
  46.         $definition = new QuantityPriceDefinition($unitPrice$taxRules$tmpItem->getQuantity());
  47.         $price $this->quantityPriceCalculator->calculate($definition$context);
  48.         $price->assign([
  49.             'listPrice' => $lineItemPrice->getListPrice() ?? null,
  50.         ]);
  51.         $tmpItem->setPrice($price);
  52.         return $tmpItem;
  53.     }
  54. }