<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Test\Testmodule\Model;
use \Magento\Framework\Exception\LocalizedException;
use \Magento\Quote\Api\CouponManagementInterface;
use \Magento\Framework\Exception\CouldNotDeleteException;
use \Magento\Framework\Exception\CouldNotSaveException;
use \Magento\Framework\Exception\NoSuchEntityException;
/**
* Coupon management object.
*/
class CouponManagement extendsimplements \Magento\Quote\Model\CouponManagementCouponManagementInterface
{
/**
* Quote repository.
*
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;
/**
* Constructs a coupon read service object.
*
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {
$this->quoteRepository = $quoteRepository;
}
/**
* {@inheritdoc}@inheritDoc
*/
public function get($cartId)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
return $quote->getCouponCode();
}
/**
* {@inheritdoc}@inheritDoc
*/
public function set($cartId, $couponCode)
{ {
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if (!$quote->getItemsCount()) {
throw new NoSuchEntityException(__('Cart'The %1"%1" Cart doesn\'t contain products'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 (LocalizedException $e) {
throw new CouldNotSaveException(__('The coupon code couldn\'t be applied: ' .$e->getMessage()), $e);
} catch (\Exception $e) {
throw new CouldNotSaveException(
__('Could"The notcoupon applycode couldn't be applied. Verify the coupon code'code and try again."),
$e
);
}
if ($quote->getCouponCode() != $couponCode) {
throw new NoSuchEntityException(__('Coupon"The codecoupon iscode notisn't valid. orVerify expired'the code and try again."));
}
return true;
}
/**
* {@inheritdoc}@inheritDoc
*/
public function remove($cartId)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if (!$quote->getItemsCount()) {
throw new NoSuchEntityException(__('Cart'The %1"%1" Cart doesn\'t contain products'products.', $cartId));
}
$quote->getShippingAddress()->setCollectShippingRates(true);
try {
$quote->setCouponCode('');
$this->quoteRepository->save($quote->collectTotals());
} catch (\Exception $e) {
throw new CouldNotDeleteException(
__('Could"The notcoupon deletecode couldn't be deleted. Verify the coupon code'code and try again.")
);
}
if ($quote->getCouponCode() != '') {
throw new CouldNotDeleteException(
__('Could"The notcoupon deletecode couldn't be deleted. Verify the coupon code'code and try again.")
);
}
return true;
}
}
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Test\Testmodule\Model;
use \Magento\Quote\Api\CouponManagementInterface;
use \Magento\Framework\Exception\CouldNotDeleteException;
use \Magento\Framework\Exception\CouldNotSaveException;
use \Magento\Framework\Exception\NoSuchEntityException;
/**
* Coupon management object.
*/
class CouponManagement extends \Magento\Quote\Model\CouponManagement
{
/**
* Quote repository.
*
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;
/**
* Constructs a coupon read service object.
*
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {
$this->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(__('Cart %1 doesn\'t contain products', $cartId));
}
$quote->getShippingAddress()->setCollectShippingRates(true);
try {
$quote->setCouponCode($couponCode);
$this->quoteRepository->save($quote->collectTotals());
} catch (\Exception $e) {
throw new CouldNotSaveException(__('Could not apply coupon code'));
}
if ($quote->getCouponCode() != $couponCode) {
throw new NoSuchEntityException(__('Coupon code is not valid or expired'));
}
return true;
}
/**
* {@inheritdoc}
*/
public function remove($cartId)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if (!$quote->getItemsCount()) {
throw new NoSuchEntityException(__('Cart %1 doesn\'t contain products', $cartId));
}
$quote->getShippingAddress()->setCollectShippingRates(true);
try {
$quote->setCouponCode('');
$this->quoteRepository->save($quote->collectTotals());
} catch (\Exception $e) {
throw new CouldNotDeleteException(__('Could not delete coupon code'));
}
if ($quote->getCouponCode() != '') {
throw new CouldNotDeleteException(__('Could not delete coupon code'));
}
return true;
}
}
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Test\Testmodule\Model;
use \Magento\Framework\Exception\LocalizedException;
use \Magento\Quote\Api\CouponManagementInterface;
use \Magento\Framework\Exception\CouldNotDeleteException;
use \Magento\Framework\Exception\CouldNotSaveException;
use \Magento\Framework\Exception\NoSuchEntityException;
/**
* Coupon management object.
*/
class CouponManagement implements CouponManagementInterface
{
/**
* Quote repository.
*
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;
/**
* Constructs a coupon read service object.
*
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {
$this->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 (LocalizedException $e) {
throw new CouldNotSaveException(__('The coupon code couldn\'t be applied: ' .$e->getMessage()), $e);
} catch (\Exception $e) {
throw new CouldNotSaveException(
__("The coupon code couldn't be applied. Verify the coupon code and try again."),
$e
);
}
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;
}
}
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Test\Testmodule\Model;
use Magento\Framework\Exception\LocalizedException;
use \Magento\Quote\Api\CouponManagementInterface;
use Magento\Framework\Exception\CouldNotDeleteException;\Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;\Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;\Magento\Framework\Exception\NoSuchEntityException;
/**
* Coupon management object.
*/
class CouponManagement implementsextends CouponManagementInterface\Magento\Quote\Model\CouponManagement
{
/**
* Quote repository.
*
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;
/**
* Constructs a coupon read service object.
*
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {
$this->quoteRepository = $quoteRepository;
}
/**
* @inheritDoc{@inheritdoc}
*/
public function get($cartId)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
return $quote->getCouponCode();
}
/**
* @inheritDoc{@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 Cart%1 doesn\'t contain products.'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 (LocalizedException $e) {
throw new CouldNotSaveException(__('The coupon code couldn\'t be applied: ' .$e->getMessage()), $e);
} catch (\Exception $e) {
throw new CouldNotSaveException(
__("The coupon code couldn't be applied.'Could Verifynot theapply coupon code and try again."code'),
$e
);
}
if ($quote->getCouponCode() != $couponCode) {
throw new NoSuchEntityException(__("The coupon'Coupon code isn't valid. Verify theis codenot andvalid tryor again."expired'));
}
return true;
}
/**
* @inheritDoc{@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 Cart%1 doesn\'t contain products.'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.'Could Verifynot thedelete coupon code and try again."code')
);
}
if ($quote->getCouponCode() != '') {
throw new CouldNotDeleteException(
__("The coupon code couldn't be deleted.'Could Verifynot thedelete coupon code and try again."code')
);
}
return true;
}
}
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Test\Testmodule\Model;
use Magento\Framework\Exception\LocalizedException;
use \Magento\Quote\Api\CouponManagementInterface;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
/**
* Coupon management object.
*/
class CouponManagement implements CouponManagementInterface
{
/**
* Quote repository.
*
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;
/**
* Constructs a coupon read service object.
*
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {
$this->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 (LocalizedException $e) {
throw new CouldNotSaveException(__('The coupon code couldn\'t be applied: ' .$e->getMessage()), $e);
} catch (\Exception $e) {
throw new CouldNotSaveException(
__("The coupon code couldn't be applied. Verify the coupon code and try again."),
$e
);
}
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;
}
}
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Test\Testmodule\Model;
use \Magento\Quote\Api\CouponManagementInterface;
use \Magento\Framework\Exception\CouldNotDeleteException;
use \Magento\Framework\Exception\CouldNotSaveException;
use \Magento\Framework\Exception\NoSuchEntityException;
/**
* Coupon management object.
*/
class CouponManagement extends \Magento\Quote\Model\CouponManagement
{
/**
* Quote repository.
*
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;
/**
* Constructs a coupon read service object.
*
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {
$this->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(__('Cart %1 doesn\'t contain products', $cartId));
}
$quote->getShippingAddress()->setCollectShippingRates(true);
try {
$quote->setCouponCode($couponCode);
$this->quoteRepository->save($quote->collectTotals());
} catch (\Exception $e) {
throw new CouldNotSaveException(__('Could not apply coupon code'));
}
if ($quote->getCouponCode() != $couponCode) {
throw new NoSuchEntityException(__('Coupon code is not valid or expired'));
}
return true;
}
/**
* {@inheritdoc}
*/
public function remove($cartId)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if (!$quote->getItemsCount()) {
throw new NoSuchEntityException(__('Cart %1 doesn\'t contain products', $cartId));
}
$quote->getShippingAddress()->setCollectShippingRates(true);
try {
$quote->setCouponCode('');
$this->quoteRepository->save($quote->collectTotals());
} catch (\Exception $e) {
throw new CouldNotDeleteException(__('Could not delete coupon code'));
}
if ($quote->getCouponCode() != '') {
throw new CouldNotDeleteException(__('Could not delete coupon code'));
}
return true;
}
}
di.xmlCreate module.xml in app/code/Test/Testmodule/etc
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Test_Testmodule" setup_version="1.0.1">
</module>
</config>
Create registration.php in app/code/Test/Testmodule and add the following code in it:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Test_Testmodule',
__DIR__
);
Create di.xml in app/code/Test/Testmodule/etc
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="\Magento\Quote\Model\CouponManagement" type="Test\TestOverride\Model\CouponManagement"type="Test\Testmodule\Model\CouponManagement" />
</config>
type="Test\TestOverride\Model\CouponManagement" write your vendor and module name
Override CouponManagement.php
CouponManagement.phpcreate CouponManagement.php in app/code/Test/Testmodule/Model and add the following code in
Test\TestOverride\Model\CouponManagement.<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Test\Testmodule\Model;
use Magento\Framework\Exception\LocalizedException;
use \Magento\Quote\Api\CouponManagementInterface;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
/**
* Coupon management object.
*/
class CouponManagement implements CouponManagementInterface
{
/**
* Quote repository.
*
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;
/**
* Constructs a coupon read service object.
*
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {
$this->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 (LocalizedException $e) {
throw new CouldNotSaveException(__('The coupon code couldn\'t be applied: ' .$e->getMessage()), $e);
} catch (\Exception $e) {
throw new CouldNotSaveException(
__("The coupon code couldn't be applied. Verify the coupon code and try again."),
$e
);
}
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;
}
}
di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="\Magento\Quote\Model\CouponManagement" type="Test\TestOverride\Model\CouponManagement" />
</config>
type="Test\TestOverride\Model\CouponManagement" write your vendor and module name
CouponManagement.php
Test\TestOverride\Model\CouponManagement.php
Create module.xml in app/code/Test/Testmodule/etc
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Test_Testmodule" setup_version="1.0.1">
</module>
</config>
Create registration.php in app/code/Test/Testmodule and add the following code in it:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Test_Testmodule',
__DIR__
);
Create di.xml in app/code/Test/Testmodule/etc
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="\Magento\Quote\Model\CouponManagement" type="Test\Testmodule\Model\CouponManagement" />
</config>
Override CouponManagement.php
create CouponManagement.php in app/code/Test/Testmodule/Model and add the following code in
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Test\Testmodule\Model;
use Magento\Framework\Exception\LocalizedException;
use \Magento\Quote\Api\CouponManagementInterface;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
/**
* Coupon management object.
*/
class CouponManagement implements CouponManagementInterface
{
/**
* Quote repository.
*
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;
/**
* Constructs a coupon read service object.
*
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository Quote repository.
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository
) {
$this->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 (LocalizedException $e) {
throw new CouldNotSaveException(__('The coupon code couldn\'t be applied: ' .$e->getMessage()), $e);
} catch (\Exception $e) {
throw new CouldNotSaveException(
__("The coupon code couldn't be applied. Verify the coupon code and try again."),
$e
);
}
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;
}
}