CartBuilder.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\Framework\DataObject;
  9. use Magento\Quote\Api\CartManagementInterface;
  10. use Magento\Quote\Api\CartManagementInterfaceFactory;
  11. use Magento\Quote\Api\CartRepositoryInterface;
  12. use Magento\Quote\Api\CartRepositoryInterfaceFactory;
  13. use Magento\Quote\Api\Data\CartItemInterface;
  14. use Magento\Quote\Api\Data\CartItemInterfaceFactory;
  15. /**
  16. * Build a Cart
  17. */
  18. class CartBuilder
  19. {
  20. /** @var CartItemInterfaceFactory */
  21. private $cartItemFactory;
  22. /** @var CartManagementInterfaceFactory */
  23. private $cartManagerFactory;
  24. /** @var CartRepositoryInterfaceFactory */
  25. private $cartRepositoryFactory;
  26. /** @var CartItemInterface[] */
  27. private $items = [];
  28. /**
  29. * @param CartItemInterfaceFactory $cartItemFactory
  30. * @param CartManagementInterfaceFactory $cartManagerFactory
  31. * @param CartRepositoryInterfaceFactory $cartRepositoryFactory
  32. */
  33. public function __construct(
  34. CartItemInterfaceFactory $cartItemFactory,
  35. CartManagementInterfaceFactory $cartManagerFactory,
  36. CartRepositoryInterfaceFactory $cartRepositoryFactory
  37. ) {
  38. $this->cartItemFactory = $cartItemFactory;
  39. $this->cartManagerFactory = $cartManagerFactory;
  40. $this->cartRepositoryFactory = $cartRepositoryFactory;
  41. }
  42. /**
  43. * Add a product to the cart
  44. *
  45. * @param ProductInterface $product
  46. * @param int $qty Default 1
  47. * @return $this
  48. */
  49. public function addItem(ProductInterface $product, $qty = 1)
  50. {
  51. /** @var CartItemInterface $cartItem */
  52. $cartItem = $this->cartItemFactory->create();
  53. $cartItem->setSku($product->getSku());
  54. $cartItem->setName($product->getName());
  55. $cartItem->setQty($qty);
  56. $cartItem->setPrice($product->getPrice());
  57. $cartItem->setProductType($product->getTypeId());
  58. $this->items[] = $cartItem;
  59. return $this;
  60. }
  61. /**
  62. * Build the Cart
  63. *
  64. * @param int $customerId
  65. * @return \Magento\Quote\Api\Data\CartInterface
  66. * @throws \Magento\Framework\Exception\CouldNotSaveException
  67. * @throws \Magento\Framework\Exception\NoSuchEntityException
  68. */
  69. public function create($customerId)
  70. {
  71. /** @var CartManagementInterface $cartManager */
  72. $cartManager = $this->cartManagerFactory->create();
  73. /** @var CartRepositoryInterface $cartRepository */
  74. $cartRepository = $this->cartRepositoryFactory->create();
  75. $cartManager->createEmptyCartForCustomer($customerId);
  76. $cart = $cartManager->getCartForCustomer($customerId);
  77. $cart->setItems($this->items);
  78. $cartRepository->save($cart);
  79. if ($cart instanceof DataObject) {
  80. $cart->setData('totals_collected_flag', false);
  81. }
  82. return $cart;
  83. }
  84. /**
  85. * Set the cart items
  86. *
  87. * @param CartItemInterface[] $items
  88. * @return $this
  89. */
  90. public function setItems(array $items = [])
  91. {
  92. $this->items = $items;
  93. return $this;
  94. }
  95. }