Purchase.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\InstantPurchase\Model\QuoteManagement;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Quote\Api\CartManagementInterface;
  9. use Magento\Quote\Api\CartRepositoryInterface;
  10. use Magento\Quote\Model\Quote;
  11. /**
  12. * Purchase products from quote.
  13. *
  14. * @api May be used for pluginization.
  15. * @since 100.2.0
  16. */
  17. class Purchase
  18. {
  19. /**
  20. * @var CartRepositoryInterface
  21. */
  22. private $quoteRepository;
  23. /**
  24. * @var CartManagementInterface
  25. */
  26. private $quoteManagement;
  27. /**
  28. * Purchase constructor.
  29. * @param CartRepositoryInterface $quoteRepository
  30. * @param CartManagementInterface $quoteManagement
  31. */
  32. public function __construct(
  33. CartRepositoryInterface $quoteRepository,
  34. CartManagementInterface $quoteManagement
  35. ) {
  36. $this->quoteRepository = $quoteRepository;
  37. $this->quoteManagement = $quoteManagement;
  38. }
  39. /**
  40. * Summarize quote and place order.
  41. *
  42. * @param Quote $quote
  43. * @return int Order id
  44. * @throws LocalizedException if order can not be placed for a quote.
  45. * @since 100.2.0
  46. */
  47. public function purchase(Quote $quote): int
  48. {
  49. $quote->collectTotals();
  50. $this->quoteRepository->save($quote);
  51. $orderId = $this->quoteManagement->placeOrder($quote->getId());
  52. return $orderId;
  53. }
  54. }