123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Checkout\Controller\Cart;
- use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class CouponPost extends \Magento\Checkout\Controller\Cart implements HttpPostActionInterface
- {
- /**
- * Sales quote repository
- *
- * @var \Magento\Quote\Api\CartRepositoryInterface
- */
- protected $quoteRepository;
- /**
- * Coupon factory
- *
- * @var \Magento\SalesRule\Model\CouponFactory
- */
- protected $couponFactory;
- /**
- * @param \Magento\Framework\App\Action\Context $context
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
- * @param \Magento\Checkout\Model\Session $checkoutSession
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
- * @param \Magento\Checkout\Model\Cart $cart
- * @param \Magento\SalesRule\Model\CouponFactory $couponFactory
- * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
- * @codeCoverageIgnore
- */
- public function __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
- ) {
- parent::__construct(
- $context,
- $scopeConfig,
- $checkoutSession,
- $storeManager,
- $formKeyValidator,
- $cart
- );
- $this->couponFactory = $couponFactory;
- $this->quoteRepository = $quoteRepository;
- }
- /**
- * Initialize coupon
- *
- * @return \Magento\Framework\Controller\Result\Redirect
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function execute()
- {
- $couponCode = $this->getRequest()->getParam('remove') == 1
- ? ''
- : trim($this->getRequest()->getParam('coupon_code'));
- $cartQuote = $this->cart->getQuote();
- $oldCouponCode = $cartQuote->getCouponCode();
- $codeLength = strlen($couponCode);
- if (!$codeLength && !strlen($oldCouponCode)) {
- return $this->_goBack();
- }
- try {
- $isCodeLengthValid = $codeLength && $codeLength <= \Magento\Checkout\Helper\Cart::COUPON_CODE_MAX_LENGTH;
- $itemsCount = $cartQuote->getItemsCount();
- if ($itemsCount) {
- $cartQuote->getShippingAddress()->setCollectShippingRates(true);
- $cartQuote->setCouponCode($isCodeLengthValid ? $couponCode : '')->collectTotals();
- $this->quoteRepository->save($cartQuote);
- }
- if ($codeLength) {
- $escaper = $this->_objectManager->get(\Magento\Framework\Escaper::class);
- $coupon = $this->couponFactory->create();
- $coupon->load($couponCode, 'code');
- if (!$itemsCount) {
- if ($isCodeLengthValid && $coupon->getId()) {
- $this->_checkoutSession->getQuote()->setCouponCode($couponCode)->save();
- $this->messageManager->addSuccessMessage(
- __(
- 'You used coupon code "%1".',
- $escaper->escapeHtml($couponCode)
- )
- );
- } else {
- $this->messageManager->addErrorMessage(
- __(
- 'The coupon code "%1" is not valid.',
- $escaper->escapeHtml($couponCode)
- )
- );
- }
- } else {
- if ($isCodeLengthValid && $coupon->getId() && $couponCode == $cartQuote->getCouponCode()) {
- $this->messageManager->addSuccessMessage(
- __(
- 'You used coupon code "%1".',
- $escaper->escapeHtml($couponCode)
- )
- );
- } else {
- $this->messageManager->addErrorMessage(
- __(
- 'The coupon code "%1" is not valid.',
- $escaper->escapeHtml($couponCode)
- )
- );
- }
- }
- } else {
- $this->messageManager->addSuccessMessage(__('You canceled the coupon code.'));
- }
- } catch (\Magento\Framework\Exception\LocalizedException $e) {
- $this->messageManager->addErrorMessage($e->getMessage());
- } catch (\Exception $e) {
- $this->messageManager->addErrorMessage(__('We cannot apply the coupon code.'));
- $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
- }
- return $this->_goBack();
- }
- }
|