CouponManagement.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Quote\Model;
  8. use \Magento\Quote\Api\CouponManagementInterface;
  9. use Magento\Framework\Exception\CouldNotDeleteException;
  10. use Magento\Framework\Exception\CouldNotSaveException;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. /**
  13. * Coupon management object.
  14. */
  15. class CouponManagement implements CouponManagementInterface
  16. {
  17. /**
  18. * Quote repository.
  19. *
  20. * @var \Magento\Quote\Api\CartRepositoryInterface
  21. */
  22. protected $quoteRepository;
  23. /**
  24. * Constructs a coupon read service object.
  25. *
  26. * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
  27. */
  28. public function __construct(
  29. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
  30. ) {
  31. $this->quoteRepository = $quoteRepository;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function get($cartId)
  37. {
  38. /** @var \Magento\Quote\Model\Quote $quote */
  39. $quote = $this->quoteRepository->getActive($cartId);
  40. return $quote->getCouponCode();
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function set($cartId, $couponCode)
  46. {
  47. /** @var \Magento\Quote\Model\Quote $quote */
  48. $quote = $this->quoteRepository->getActive($cartId);
  49. if (!$quote->getItemsCount()) {
  50. throw new NoSuchEntityException(__('The "%1" Cart doesn\'t contain products.', $cartId));
  51. }
  52. if (!$quote->getStoreId()) {
  53. throw new NoSuchEntityException(__('Cart isn\'t assigned to correct store'));
  54. }
  55. $quote->getShippingAddress()->setCollectShippingRates(true);
  56. try {
  57. $quote->setCouponCode($couponCode);
  58. $this->quoteRepository->save($quote->collectTotals());
  59. } catch (\Exception $e) {
  60. throw new CouldNotSaveException(
  61. __("The coupon code couldn't be applied. Verify the coupon code and try again.")
  62. );
  63. }
  64. if ($quote->getCouponCode() != $couponCode) {
  65. throw new NoSuchEntityException(__("The coupon code isn't valid. Verify the code and try again."));
  66. }
  67. return true;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function remove($cartId)
  73. {
  74. /** @var \Magento\Quote\Model\Quote $quote */
  75. $quote = $this->quoteRepository->getActive($cartId);
  76. if (!$quote->getItemsCount()) {
  77. throw new NoSuchEntityException(__('The "%1" Cart doesn\'t contain products.', $cartId));
  78. }
  79. $quote->getShippingAddress()->setCollectShippingRates(true);
  80. try {
  81. $quote->setCouponCode('');
  82. $this->quoteRepository->save($quote->collectTotals());
  83. } catch (\Exception $e) {
  84. throw new CouldNotDeleteException(
  85. __("The coupon code couldn't be deleted. Verify the coupon code and try again.")
  86. );
  87. }
  88. if ($quote->getCouponCode() != '') {
  89. throw new CouldNotDeleteException(
  90. __("The coupon code couldn't be deleted. Verify the coupon code and try again.")
  91. );
  92. }
  93. return true;
  94. }
  95. }