quoteRepository = $quoteRepository; } /** * {@inheritdoc} */ public function get($cartId) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); return $quote->getCouponCode(); } /** * {@inheritdoc} */ public function set($cartId, $couponCode) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if (!$quote->getItemsCount()) { throw new NoSuchEntityException(__('The "%1" Cart doesn\'t contain products.', $cartId)); } if (!$quote->getStoreId()) { throw new NoSuchEntityException(__('Cart isn\'t assigned to correct store')); } $quote->getShippingAddress()->setCollectShippingRates(true); try { $quote->setCouponCode($couponCode); $this->quoteRepository->save($quote->collectTotals()); } catch (\Exception $e) { throw new CouldNotSaveException( __("The coupon code couldn't be applied. Verify the coupon code and try again.") ); } if ($quote->getCouponCode() != $couponCode) { throw new NoSuchEntityException(__("The coupon code isn't valid. Verify the code and try again.")); } return true; } /** * {@inheritdoc} */ public function remove($cartId) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if (!$quote->getItemsCount()) { throw new NoSuchEntityException(__('The "%1" Cart doesn\'t contain products.', $cartId)); } $quote->getShippingAddress()->setCollectShippingRates(true); try { $quote->setCouponCode(''); $this->quoteRepository->save($quote->collectTotals()); } catch (\Exception $e) { throw new CouldNotDeleteException( __("The coupon code couldn't be deleted. Verify the coupon code and try again.") ); } if ($quote->getCouponCode() != '') { throw new CouldNotDeleteException( __("The coupon code couldn't be deleted. Verify the coupon code and try again.") ); } return true; } }