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

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\Struct\Struct;
  20. class Cart extends Struct
  21. {
  22.     /**
  23.      * @var string
  24.      */
  25.     protected $name;
  26.     /**
  27.      * @var string
  28.      */
  29.     protected $token;
  30.     /**
  31.      * @var CartPrice
  32.      */
  33.     protected $price;
  34.     /**
  35.      * @var LineItemCollection
  36.      */
  37.     protected $lineItems;
  38.     /**
  39.      * @var ErrorCollection
  40.      */
  41.     protected $errors;
  42.     /**
  43.      * @var DeliveryCollection
  44.      */
  45.     protected $deliveries;
  46.     /**
  47.      * @var TransactionCollection
  48.      */
  49.     protected $transactions;
  50.     /**
  51.      * @var bool
  52.      */
  53.     protected $modified false;
  54.     /**
  55.      * @var string|null
  56.      */
  57.     protected $customerComment;
  58.     /**
  59.      * @var string|null
  60.      */
  61.     protected $affiliateCode;
  62.     /**
  63.      * @var string|null
  64.      */
  65.     protected $campaignCode;
  66.     /**
  67.      * @var CartDataCollection|null
  68.      */
  69.     private $data;
  70.     public function __construct(string $namestring $token)
  71.     {
  72.         $this->name $name;
  73.         $this->token $token;
  74.         $this->lineItems = new LineItemCollection();
  75.         $this->transactions = new TransactionCollection();
  76.         $this->errors = new ErrorCollection();
  77.         $this->deliveries = new DeliveryCollection();
  78.         $this->price = new CartPrice(000, new CalculatedTaxCollection(), new TaxRuleCollection(), CartPrice::TAX_STATE_GROSS);
  79.     }
  80.     public function getName(): string
  81.     {
  82.         return $this->name;
  83.     }
  84.     public function setName(string $name): void
  85.     {
  86.         $this->name $name;
  87.     }
  88.     public function getToken(): string
  89.     {
  90.         return $this->token;
  91.     }
  92.     public function setToken(string $token): void
  93.     {
  94.         $this->token $token;
  95.     }
  96.     public function getLineItems(): LineItemCollection
  97.     {
  98.         return $this->lineItems;
  99.     }
  100.     public function setLineItems(LineItemCollection $lineItems): void
  101.     {
  102.         $this->lineItems $lineItems;
  103.     }
  104.     public function getErrors(): ErrorCollection
  105.     {
  106.         return $this->errors;
  107.     }
  108.     public function setErrors(ErrorCollection $errors): void
  109.     {
  110.         $this->errors $errors;
  111.     }
  112.     public function getDeliveries(): DeliveryCollection
  113.     {
  114.         return $this->deliveries;
  115.     }
  116.     public function setDeliveries(DeliveryCollection $deliveries): void
  117.     {
  118.         $this->deliveries $deliveries;
  119.     }
  120.     /**
  121.      * @throws InvalidQuantityException
  122.      * @throws LineItemNotStackableException
  123.      * @throws MixedLineItemTypeException
  124.      */
  125.     public function addLineItems(LineItemCollection $lineItems): void
  126.     {
  127.         foreach ($lineItems as $lineItem) {
  128.             $this->add($lineItem);
  129.         }
  130.     }
  131.     public function addDeliveries(DeliveryCollection $deliveries): void
  132.     {
  133.         foreach ($deliveries as $delivery) {
  134.             $this->deliveries->add($delivery);
  135.         }
  136.     }
  137.     public function addErrors(Error ...$errors): void
  138.     {
  139.         foreach ($errors as $error) {
  140.             $this->errors->add($error);
  141.         }
  142.     }
  143.     public function getPrice(): CartPrice
  144.     {
  145.         return $this->price;
  146.     }
  147.     public function setPrice(CartPrice $price): void
  148.     {
  149.         $this->price $price;
  150.     }
  151.     /**
  152.      * @throws InvalidQuantityException
  153.      * @throws LineItemNotStackableException
  154.      * @throws MixedLineItemTypeException
  155.      */
  156.     public function add(LineItem $lineItem): self
  157.     {
  158.         $this->lineItems->add($lineItem);
  159.         return $this;
  160.     }
  161.     public function get(string $lineItemKey)
  162.     {
  163.         return $this->lineItems->get($lineItemKey);
  164.     }
  165.     public function has(string $lineItemKey): bool
  166.     {
  167.         return $this->lineItems->has($lineItemKey);
  168.     }
  169.     /**
  170.      * @throws LineItemNotFoundException
  171.      * @throws LineItemNotRemovableException
  172.      */
  173.     public function remove(string $key): void
  174.     {
  175.         if (!$this->has($key)) {
  176.             throw new LineItemNotFoundException($key);
  177.         }
  178.         if (!$this->get($key)->isRemovable()) {
  179.             throw new LineItemNotRemovableException($key);
  180.         }
  181.         $this->lineItems->remove($key);
  182.     }
  183.     public function getTransactions(): TransactionCollection
  184.     {
  185.         return $this->transactions;
  186.     }
  187.     public function setTransactions(TransactionCollection $transactions): self
  188.     {
  189.         $this->transactions $transactions;
  190.         return $this;
  191.     }
  192.     public function getShippingCosts(): CalculatedPrice
  193.     {
  194.         return $this->deliveries->getShippingCosts()->sum();
  195.     }
  196.     public function getData(): CartDataCollection
  197.     {
  198.         if (!$this->data) {
  199.             $this->data = new CartDataCollection();
  200.         }
  201.         return $this->data;
  202.     }
  203.     public function setData(?CartDataCollection $data): void
  204.     {
  205.         $this->data $data;
  206.     }
  207.     public function isModified(): bool
  208.     {
  209.         return $this->modified;
  210.     }
  211.     public function markModified(): void
  212.     {
  213.         $this->modified true;
  214.     }
  215.     public function markUnmodified(): void
  216.     {
  217.         $this->modified false;
  218.     }
  219.     public function getCustomerComment(): ?string
  220.     {
  221.         return $this->customerComment;
  222.     }
  223.     public function setCustomerComment(?string $customerComment): void
  224.     {
  225.         $this->customerComment $customerComment;
  226.     }
  227.     public function getAffiliateCode(): ?string
  228.     {
  229.         return $this->affiliateCode;
  230.     }
  231.     public function setAffiliateCode(?string $affiliateCode): void
  232.     {
  233.         $this->affiliateCode $affiliateCode;
  234.     }
  235.     public function getCampaignCode(): ?string
  236.     {
  237.         return $this->campaignCode;
  238.     }
  239.     public function setCampaignCode(?string $campaignCode): void
  240.     {
  241.         $this->campaignCode $campaignCode;
  242.     }
  243.     public function getApiAlias(): string
  244.     {
  245.         return 'cart';
  246.     }
  247. }