![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/module-sales-rule/Observer/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ declare(strict_types=1); namespace Magento\SalesRule\Observer; use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Framework\Event\Observer as EventObserver; use Magento\Framework\Event\ObserverInterface; use Magento\Quote\Api\CartRepositoryInterface; use Magento\Quote\Api\Data\CartInterface; use Magento\Quote\Model\Quote; use Magento\SalesRule\Api\Exception\CodeRequestLimitException; use Magento\SalesRule\Model\Spi\CodeLimitManagerInterface; /** * Validate newly provided coupon code before using it while calculating totals. */ class CouponCodeValidation implements ObserverInterface { /** * @var CartRepositoryInterface */ private $cartRepository; /** * @var SearchCriteriaBuilder */ private $criteriaBuilder; /** * @var CodeLimitManagerInterface */ private $codeLimitManager; /** * @param CodeLimitManagerInterface $codeLimitManager * @param CartRepositoryInterface $cartRepository * @param SearchCriteriaBuilder $criteriaBuilder */ public function __construct( CodeLimitManagerInterface $codeLimitManager, CartRepositoryInterface $cartRepository, SearchCriteriaBuilder $criteriaBuilder ) { $this->codeLimitManager = $codeLimitManager; $this->cartRepository = $cartRepository; $this->criteriaBuilder = $criteriaBuilder; } /** * @inheritDoc */ public function execute(EventObserver $observer) { /** @var Quote $quote */ $quote = $observer->getData('quote'); $code = $quote->getCouponCode(); if ($code) { //Only validating the code if it's a new code. /** @var Quote[] $found */ $found = $this->cartRepository->getList( $this->criteriaBuilder->addFilter('main_table.' . CartInterface::KEY_ENTITY_ID, $quote->getId()) ->create() )->getItems(); if (!$found || ((string)array_shift($found)->getCouponCode()) !== (string)$code) { try { $this->codeLimitManager->checkRequest($code); } catch (CodeRequestLimitException $exception) { $quote->setCouponCode(''); throw $exception; } } } } }