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

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