Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
CouponPost Class Reference
Inheritance diagram for CouponPost:
Cart HttpPostActionInterface Action ViewInterface ActionInterface AbstractAction ActionInterface

Public Member Functions

 __construct (\Magento\Framework\App\Action\Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator, \Magento\Checkout\Model\Cart $cart, \Magento\SalesRule\Model\CouponFactory $couponFactory, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository)
 
 execute ()
 
- Public Member Functions inherited from Cart
 __construct (\Magento\Framework\App\Action\Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator, CustomerCart $cart)
 
- Public Member Functions inherited from Action
 __construct (Context $context)
 
 dispatch (RequestInterface $request)
 
 getActionFlag ()
 
- Public Member Functions inherited from AbstractAction
 __construct (Context $context)
 
 dispatch (RequestInterface $request)
 
 getRequest ()
 
 getResponse ()
 

Protected Attributes

 $quoteRepository
 
 $couponFactory
 
- Protected Attributes inherited from Cart
 $_scopeConfig
 
 $_checkoutSession
 
 $_storeManager
 
 $_formKeyValidator
 
 $cart
 
- Protected Attributes inherited from Action
 $_objectManager
 
 $_sessionNamespace
 
 $_eventManager
 
 $_actionFlag
 
 $_redirect
 
 $_view
 
 $_url
 
 $messageManager
 
- Protected Attributes inherited from AbstractAction
 $_request
 
 $_response
 
 $resultRedirectFactory
 
 $resultFactory
 

Additional Inherited Members

- Data Fields inherited from ActionInterface
const FLAG_NO_DISPATCH = 'no-dispatch'
 
const FLAG_NO_POST_DISPATCH = 'no-postDispatch'
 
const FLAG_NO_DISPATCH_BLOCK_EVENT = 'no-beforeGenerateLayoutBlocksDispatch'
 
const PARAM_NAME_BASE64_URL = 'r64'
 
const PARAM_NAME_URL_ENCODED = 'uenc'
 
- Protected Member Functions inherited from Cart
 _goBack ($backUrl=null)
 
 getBackUrl ($defaultUrl=null)
 
- Protected Member Functions inherited from Action
 _forward ($action, $controller=null, $module=null, array $params=null)
 
 _redirect ($path, $arguments=[])
 

Detailed Description

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 13 of file CouponPost.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\App\Action\Context  $context,
\Magento\Framework\App\Config\ScopeConfigInterface  $scopeConfig,
\Magento\Checkout\Model\Session  $checkoutSession,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Framework\Data\Form\FormKey\Validator  $formKeyValidator,
\Magento\Checkout\Model\Cart  $cart,
\Magento\SalesRule\Model\CouponFactory  $couponFactory,
\Magento\Quote\Api\CartRepositoryInterface  $quoteRepository 
)
Parameters
\Magento\Framework\App\Action\Context$context
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig
\Magento\Checkout\Model\Session$checkoutSession
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Framework\Data\Form\FormKey\Validator$formKeyValidator
\Magento\Checkout\Model\Cart$cart
\Magento\SalesRule\Model\CouponFactory$couponFactory
\Magento\Quote\Api\CartRepositoryInterface$quoteRepository@codeCoverageIgnore

Definition at line 40 of file CouponPost.php.

49  {
50  parent::__construct(
51  $context,
52  $scopeConfig,
53  $checkoutSession,
55  $formKeyValidator,
56  $cart
57  );
58  $this->couponFactory = $couponFactory;
59  $this->quoteRepository = $quoteRepository;
60  }
$quoteRepository
Definition: CouponPost.php:20
$storeManager
$couponFactory
Definition: CouponPost.php:27

Member Function Documentation

◆ execute()

execute ( )

Initialize coupon

Returns
\Magento\Framework\Controller\Result\Redirect @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Implements ActionInterface.

Definition at line 69 of file CouponPost.php.

70  {
71  $couponCode = $this->getRequest()->getParam('remove') == 1
72  ? ''
73  : trim($this->getRequest()->getParam('coupon_code'));
74 
75  $cartQuote = $this->cart->getQuote();
76  $oldCouponCode = $cartQuote->getCouponCode();
77 
78  $codeLength = strlen($couponCode);
79  if (!$codeLength && !strlen($oldCouponCode)) {
80  return $this->_goBack();
81  }
82 
83  try {
84  $isCodeLengthValid = $codeLength && $codeLength <= \Magento\Checkout\Helper\Cart::COUPON_CODE_MAX_LENGTH;
85 
86  $itemsCount = $cartQuote->getItemsCount();
87  if ($itemsCount) {
88  $cartQuote->getShippingAddress()->setCollectShippingRates(true);
89  $cartQuote->setCouponCode($isCodeLengthValid ? $couponCode : '')->collectTotals();
90  $this->quoteRepository->save($cartQuote);
91  }
92 
93  if ($codeLength) {
94  $escaper = $this->_objectManager->get(\Magento\Framework\Escaper::class);
95  $coupon = $this->couponFactory->create();
96  $coupon->load($couponCode, 'code');
97  if (!$itemsCount) {
98  if ($isCodeLengthValid && $coupon->getId()) {
99  $this->_checkoutSession->getQuote()->setCouponCode($couponCode)->save();
100  $this->messageManager->addSuccessMessage(
101  __(
102  'You used coupon code "%1".',
103  $escaper->escapeHtml($couponCode)
104  )
105  );
106  } else {
107  $this->messageManager->addErrorMessage(
108  __(
109  'The coupon code "%1" is not valid.',
110  $escaper->escapeHtml($couponCode)
111  )
112  );
113  }
114  } else {
115  if ($isCodeLengthValid && $coupon->getId() && $couponCode == $cartQuote->getCouponCode()) {
116  $this->messageManager->addSuccessMessage(
117  __(
118  'You used coupon code "%1".',
119  $escaper->escapeHtml($couponCode)
120  )
121  );
122  } else {
123  $this->messageManager->addErrorMessage(
124  __(
125  'The coupon code "%1" is not valid.',
126  $escaper->escapeHtml($couponCode)
127  )
128  );
129  }
130  }
131  } else {
132  $this->messageManager->addSuccessMessage(__('You canceled the coupon code.'));
133  }
134  } catch (\Magento\Framework\Exception\LocalizedException $e) {
135  $this->messageManager->addErrorMessage($e->getMessage());
136  } catch (\Exception $e) {
137  $this->messageManager->addErrorMessage(__('We cannot apply the coupon code.'));
138  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
139  }
140 
141  return $this->_goBack();
142  }
$coupon
__()
Definition: __.php:13
_goBack($backUrl=null)
Definition: Cart.php:73

Field Documentation

◆ $couponFactory

$couponFactory
protected

Definition at line 27 of file CouponPost.php.

◆ $quoteRepository

$quoteRepository
protected

Definition at line 20 of file CouponPost.php.


The documentation for this class was generated from the following file: