vendor/shopware/core/Checkout/Cart/Cart.php line 27

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart;
  3. use Shopware\Core\Checkout\Cart\Delivery\Struct\DeliveryCollection;
  4. use Shopware\Core\Checkout\Cart\Error\Error;
  5. use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
  6. use Shopware\Core\Checkout\Cart\Exception\InvalidQuantityException;
  7. use Shopware\Core\Checkout\Cart\Exception\LineItemNotFoundException;
  8. use Shopware\Core\Checkout\Cart\Exception\LineItemNotRemovableException;
  9. use Shopware\Core\Checkout\Cart\Exception\LineItemNotStackableException;
  10. use Shopware\Core\Checkout\Cart\Exception\MixedLineItemTypeException;
  11. use Shopware\Core\Checkout\Cart\LineItem\CartDataCollection;
  12. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  13. use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
  14. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  15. use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
  16. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
  17. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  18. use Shopware\Core\Checkout\Cart\Transaction\Struct\TransactionCollection;
  19. use Shopware\Core\Framework\Feature;
  20. use Shopware\Core\Framework\Log\Package;
  21. use Shopware\Core\Framework\Struct\StateAwareTrait;
  22. use Shopware\Core\Framework\Struct\Struct;
  23. #[Package('checkout')]
  24. class Cart extends Struct
  25. {
  26.     use StateAwareTrait;
  27.     /**
  28.      * @var string
  29.      */
  30.     protected $name;
  31.     /**
  32.      * @var string
  33.      */
  34.     protected $token;
  35.     /**
  36.      * @var CartPrice
  37.      */
  38.     protected $price;
  39.     /**
  40.      * @var LineItemCollection
  41.      */
  42.     protected $lineItems;
  43.     /**
  44.      * @var ErrorCollection
  45.      */
  46.     protected $errors;
  47.     /**
  48.      * @var DeliveryCollection
  49.      */
  50.     protected $deliveries;
  51.     /**
  52.      * @var TransactionCollection
  53.      */
  54.     protected $transactions;
  55.     /**
  56.      * @var bool
  57.      */
  58.     protected $modified false;
  59.     /**
  60.      * @var string|null
  61.      */
  62.     protected $customerComment;
  63.     /**
  64.      * @var string|null
  65.      */
  66.     protected $affiliateCode;
  67.     /**
  68.      * @var string|null
  69.      */
  70.     protected $campaignCode;
  71.     /**
  72.      * @var CartDataCollection|null
  73.      */
  74.     private $data;
  75.     /**
  76.      * @var array<string>
  77.      */
  78.     private array $ruleIds = [];
  79.     private ?CartBehavior $behavior null;
  80.     /**
  81.      * @internal
  82.      */
  83.     public function __construct(string $namestring $token)
  84.     {
  85.         $this->name $name;
  86.         $this->token $token;
  87.         $this->lineItems = new LineItemCollection();
  88.         $this->transactions = new TransactionCollection();
  89.         $this->errors = new ErrorCollection();
  90.         $this->deliveries = new DeliveryCollection();
  91.         $this->price = new CartPrice(000, new CalculatedTaxCollection(), new TaxRuleCollection(), CartPrice::TAX_STATE_GROSS);
  92.     }
  93.     public function getName(): string
  94.     {
  95.         return $this->name;
  96.     }
  97.     public function setName(string $name): void
  98.     {
  99.         $this->name $name;
  100.     }
  101.     public function getToken(): string
  102.     {
  103.         return $this->token;
  104.     }
  105.     public function setToken(string $token): void
  106.     {
  107.         $this->token $token;
  108.     }
  109.     public function getLineItems(): LineItemCollection
  110.     {
  111.         return $this->lineItems;
  112.     }
  113.     public function setLineItems(LineItemCollection $lineItems): void
  114.     {
  115.         $this->lineItems $lineItems;
  116.     }
  117.     public function getErrors(): ErrorCollection
  118.     {
  119.         return $this->errors;
  120.     }
  121.     public function setErrors(ErrorCollection $errors): void
  122.     {
  123.         $this->errors $errors;
  124.     }
  125.     public function getDeliveries(): DeliveryCollection
  126.     {
  127.         return $this->deliveries;
  128.     }
  129.     public function setDeliveries(DeliveryCollection $deliveries): void
  130.     {
  131.         $this->deliveries $deliveries;
  132.     }
  133.     /**
  134.      * @throws InvalidQuantityException
  135.      * @throws LineItemNotStackableException
  136.      * @throws MixedLineItemTypeException
  137.      */
  138.     public function addLineItems(LineItemCollection $lineItems): void
  139.     {
  140.         foreach ($lineItems as $lineItem) {
  141.             $this->add($lineItem);
  142.         }
  143.     }
  144.     public function addDeliveries(DeliveryCollection $deliveries): void
  145.     {
  146.         foreach ($deliveries as $delivery) {
  147.             $this->deliveries->add($delivery);
  148.         }
  149.     }
  150.     public function addErrors(Error ...$errors): void
  151.     {
  152.         foreach ($errors as $error) {
  153.             $this->errors->add($error);
  154.         }
  155.     }
  156.     public function getPrice(): CartPrice
  157.     {
  158.         return $this->price;
  159.     }
  160.     public function setPrice(CartPrice $price): void
  161.     {
  162.         $this->price $price;
  163.     }
  164.     /**
  165.      * @throws InvalidQuantityException
  166.      * @throws LineItemNotStackableException
  167.      * @throws MixedLineItemTypeException
  168.      */
  169.     public function add(LineItem $lineItem): self
  170.     {
  171.         $this->lineItems->add($lineItem);
  172.         return $this;
  173.     }
  174.     /**
  175.      * @return LineItem|null
  176.      */
  177.     public function get(string $lineItemKey)
  178.     {
  179.         return $this->lineItems->get($lineItemKey);
  180.     }
  181.     public function has(string $lineItemKey): bool
  182.     {
  183.         return $this->lineItems->has($lineItemKey);
  184.     }
  185.     /**
  186.      * @throws LineItemNotFoundException
  187.      * @throws LineItemNotRemovableException
  188.      */
  189.     public function remove(string $key): void
  190.     {
  191.         $item $this->get($key);
  192.         if (!$item) {
  193.             if (Feature::isActive('v6.5.0.0')) {
  194.                 throw CartException::lineItemNotFound($key);
  195.             }
  196.             throw new LineItemNotFoundException($key);
  197.         }
  198.         if (!$item->isRemovable()) {
  199.             if (Feature::isActive('v6.5.0.0')) {
  200.                 throw CartException::lineItemNotRemovable($key);
  201.             }
  202.             throw new LineItemNotRemovableException($key);
  203.         }
  204.         $this->lineItems->remove($key);
  205.     }
  206.     public function getTransactions(): TransactionCollection
  207.     {
  208.         return $this->transactions;
  209.     }
  210.     public function setTransactions(TransactionCollection $transactions): self
  211.     {
  212.         $this->transactions $transactions;
  213.         return $this;
  214.     }
  215.     public function getShippingCosts(): CalculatedPrice
  216.     {
  217.         return $this->deliveries->getShippingCosts()->sum();
  218.     }
  219.     public function getData(): CartDataCollection
  220.     {
  221.         if (!$this->data) {
  222.             $this->data = new CartDataCollection();
  223.         }
  224.         return $this->data;
  225.     }
  226.     public function setData(?CartDataCollection $data): void
  227.     {
  228.         $this->data $data;
  229.     }
  230.     public function isModified(): bool
  231.     {
  232.         return $this->modified;
  233.     }
  234.     public function markModified(): void
  235.     {
  236.         $this->modified true;
  237.     }
  238.     public function markUnmodified(): void
  239.     {
  240.         $this->modified false;
  241.     }
  242.     public function getCustomerComment(): ?string
  243.     {
  244.         return $this->customerComment;
  245.     }
  246.     public function setCustomerComment(?string $customerComment): void
  247.     {
  248.         $this->customerComment $customerComment;
  249.     }
  250.     public function getAffiliateCode(): ?string
  251.     {
  252.         return $this->affiliateCode;
  253.     }
  254.     public function setAffiliateCode(?string $affiliateCode): void
  255.     {
  256.         $this->affiliateCode $affiliateCode;
  257.     }
  258.     public function getCampaignCode(): ?string
  259.     {
  260.         return $this->campaignCode;
  261.     }
  262.     public function setCampaignCode(?string $campaignCode): void
  263.     {
  264.         $this->campaignCode $campaignCode;
  265.     }
  266.     public function getApiAlias(): string
  267.     {
  268.         return 'cart';
  269.     }
  270.     /**
  271.      * @param array<string> $ruleIds
  272.      */
  273.     public function setRuleIds(array $ruleIds): void
  274.     {
  275.         $this->ruleIds $ruleIds;
  276.     }
  277.     /**
  278.      * @return array<string>
  279.      */
  280.     public function getRuleIds(): array
  281.     {
  282.         return $this->ruleIds;
  283.     }
  284.     /**
  285.      * Will be available after the cart gets calculated
  286.      * The `\Shopware\Core\Checkout\Cart\Processor::process` will set this
  287.      */
  288.     public function getBehavior(): ?CartBehavior
  289.     {
  290.         return $this->behavior;
  291.     }
  292.     /**
  293.      * @internal These function is reserved for the `\Shopware\Core\Checkout\Cart\Processor::process`
  294.      */
  295.     public function setBehavior(?CartBehavior $behavior): void
  296.     {
  297.         $this->behavior $behavior;
  298.     }
  299. }