GuestCartManagement.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\GuestCart;
  7. use Magento\Quote\Api\GuestCartManagementInterface;
  8. use Magento\Quote\Api\CartManagementInterface;
  9. use Magento\Quote\Model\QuoteIdMask;
  10. use Magento\Quote\Model\QuoteIdMaskFactory;
  11. use Magento\Quote\Api\Data\PaymentInterface;
  12. use Magento\Quote\Api\CartRepositoryInterface;
  13. /**
  14. * Cart Management class for guest carts.
  15. *
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. */
  18. class GuestCartManagement implements GuestCartManagementInterface
  19. {
  20. /**
  21. * @var CartManagementInterface
  22. */
  23. protected $quoteManagement;
  24. /**
  25. * @var QuoteIdMaskFactory
  26. */
  27. protected $quoteIdMaskFactory;
  28. /**
  29. * @var CartRepositoryInterface
  30. */
  31. protected $cartRepository;
  32. /**
  33. * Initialize dependencies.
  34. *
  35. * @param CartManagementInterface $quoteManagement
  36. * @param QuoteIdMaskFactory $quoteIdMaskFactory
  37. * @param CartRepositoryInterface $cartRepository
  38. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  39. */
  40. public function __construct(
  41. CartManagementInterface $quoteManagement,
  42. QuoteIdMaskFactory $quoteIdMaskFactory,
  43. CartRepositoryInterface $cartRepository
  44. ) {
  45. $this->quoteManagement = $quoteManagement;
  46. $this->quoteIdMaskFactory = $quoteIdMaskFactory;
  47. $this->cartRepository = $cartRepository;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function createEmptyCart()
  53. {
  54. /** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */
  55. $quoteIdMask = $this->quoteIdMaskFactory->create();
  56. $cartId = $this->quoteManagement->createEmptyCart();
  57. $quoteIdMask->setQuoteId($cartId)->save();
  58. return $quoteIdMask->getMaskedId();
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function assignCustomer($cartId, $customerId, $storeId)
  64. {
  65. /** @var $quoteIdMask QuoteIdMask */
  66. $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
  67. return $this->quoteManagement->assignCustomer($quoteIdMask->getQuoteId(), $customerId, $storeId);
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function placeOrder($cartId, PaymentInterface $paymentMethod = null)
  73. {
  74. /** @var $quoteIdMask QuoteIdMask */
  75. $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
  76. $this->cartRepository->get($quoteIdMask->getQuoteId())
  77. ->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
  78. return $this->quoteManagement->placeOrder($quoteIdMask->getQuoteId(), $paymentMethod);
  79. }
  80. }