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

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