ApplyCouponToCart.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\QuoteGraphQl\Model\Resolver;
  8. use Magento\Framework\Exception\CouldNotSaveException;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Magento\Framework\GraphQl\Config\Element\Field;
  12. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  13. use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
  14. use Magento\Framework\GraphQl\Query\ResolverInterface;
  15. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  16. use Magento\Quote\Api\CouponManagementInterface;
  17. use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
  18. /**
  19. * @inheritdoc
  20. */
  21. class ApplyCouponToCart implements ResolverInterface
  22. {
  23. /**
  24. * @var GetCartForUser
  25. */
  26. private $getCartForUser;
  27. /**
  28. * @var CouponManagementInterface
  29. */
  30. private $couponManagement;
  31. /**
  32. * @param GetCartForUser $getCartForUser
  33. * @param CouponManagementInterface $couponManagement
  34. */
  35. public function __construct(
  36. GetCartForUser $getCartForUser,
  37. CouponManagementInterface $couponManagement
  38. ) {
  39. $this->getCartForUser = $getCartForUser;
  40. $this->couponManagement = $couponManagement;
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
  46. {
  47. if (!isset($args['input']['cart_id'])) {
  48. throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
  49. }
  50. $maskedCartId = $args['input']['cart_id'];
  51. if (!isset($args['input']['coupon_code'])) {
  52. throw new GraphQlInputException(__('Required parameter "coupon_code" is missing'));
  53. }
  54. $couponCode = $args['input']['coupon_code'];
  55. $currentUserId = $context->getUserId();
  56. $cart = $this->getCartForUser->execute($maskedCartId, $currentUserId);
  57. $cartId = $cart->getId();
  58. /* Check current cart does not have coupon code applied */
  59. $appliedCouponCode = $this->couponManagement->get($cartId);
  60. if (!empty($appliedCouponCode)) {
  61. throw new GraphQlInputException(
  62. __('A coupon is already applied to the cart. Please remove it to apply another')
  63. );
  64. }
  65. try {
  66. $this->couponManagement->set($cartId, $couponCode);
  67. } catch (NoSuchEntityException $exception) {
  68. throw new GraphQlNoSuchEntityException(__($exception->getMessage()));
  69. } catch (CouldNotSaveException $exception) {
  70. throw new LocalizedException(__($exception->getMessage()));
  71. }
  72. $data['cart']['applied_coupon'] = [
  73. 'code' => $couponCode,
  74. ];
  75. return $data;
  76. }
  77. }