GuestCartBuilder.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Test\Integration\Builder;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. use Magento\Quote\Api\CartRepositoryInterface;
  9. use Magento\Quote\Api\Data\CartInterface;
  10. use Magento\Quote\Api\Data\CartItemInterface;
  11. use Magento\Quote\Api\Data\CartItemInterfaceFactory;
  12. use Magento\Quote\Api\GuestCartManagementInterface;
  13. use Magento\Quote\Api\GuestCartRepositoryInterface;
  14. /**
  15. * Build a Guest Cart
  16. */
  17. class GuestCartBuilder
  18. {
  19. /** @var CartItemInterfaceFactory */
  20. private $cartItemFactory;
  21. /** @var GuestCartManagementInterface */
  22. private $cartManager;
  23. /** @var CartRepositoryInterface */
  24. private $cartRepository;
  25. /** @var GuestCartRepositoryInterface */
  26. private $guestCartRepository;
  27. /** @var CartItemInterface[] */
  28. private $items = [];
  29. /**
  30. * @param CartItemInterfaceFactory $cartItemFactory
  31. * @param GuestCartManagementInterface $cartManager
  32. * @param GuestCartRepositoryInterface $guestCartRepository
  33. * @param CartRepositoryInterface $cartRepository
  34. */
  35. public function __construct(
  36. CartItemInterfaceFactory $cartItemFactory,
  37. GuestCartManagementInterface $cartManager,
  38. GuestCartRepositoryInterface $guestCartRepository,
  39. CartRepositoryInterface $cartRepository
  40. ) {
  41. $this->cartItemFactory = $cartItemFactory;
  42. $this->cartManager = $cartManager;
  43. $this->guestCartRepository = $guestCartRepository;
  44. $this->cartRepository = $cartRepository;
  45. }
  46. /**
  47. * Add a product to the cart
  48. *
  49. * @param ProductInterface $product
  50. * @param int $qty Default 1
  51. * @return $this
  52. */
  53. public function addItem(ProductInterface $product, $qty = 1)
  54. {
  55. $cartItem = $this->cartItemFactory->create();
  56. $cartItem->setSku($product->getSku());
  57. $cartItem->setName($product->getName());
  58. $cartItem->setQty($qty);
  59. $cartItem->setPrice($product->getPrice());
  60. $cartItem->setProductType($product->getTypeId());
  61. $this->items[] = $cartItem;
  62. return $this;
  63. }
  64. /**
  65. * Build the Cart
  66. *
  67. * @return \Magento\Quote\Api\Data\CartInterface
  68. * @throws \Magento\Framework\Exception\CouldNotSaveException
  69. * @throws \Magento\Framework\Exception\NoSuchEntityException
  70. */
  71. public function create()
  72. {
  73. $cartId = $this->cartManager->createEmptyCart();
  74. $cart = $this->guestCartRepository->get($cartId);
  75. $cart->setItems($this->items);
  76. $this->cartRepository->save($cart);
  77. return $cart;
  78. }
  79. /**
  80. * Set the cart items
  81. *
  82. * @param CartInterface[] $items
  83. * @return $this
  84. */
  85. public function setItems(array $items = [])
  86. {
  87. $this->items = $items;
  88. return $this;
  89. }
  90. }