I want to create coupon code for specific product and custom option


Sku: Test1

Custom Option:

Name: Demo Custom Option

Option Type: Field

I want to apply the coupon code if customer have enter the name, if not filled customer will not allow to apply the coupon.


The solution is to use a plugin on Magento\Quote\Model\CouponManagement on the set method:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\CouponManagement">
        <plugin name="checkout_coupon_apply"

Create plugin:


namespace Vendor\Module\Plugin;

class CheckoutCouponApply {

         * 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;

        public function beforeSet(CouponManagement $subject, $cartId, $couponCode)
             foreach ($this->quoteRepository->getAllItems() as $value) {
                // Do what you need to do 
                // throw new CouldNotSaveException(__('You will not allow to use coupon code'));

  • I followed this plugin but not working. what is the reason behind this? – Chirag Patel Jul 8 at 6:13
  • did you faced any error? – Ashish Viradiya Jul 8 at 6:17
  • No error showing but doesn't reflect. – Chirag Patel Jul 8 at 6:20

You can create coupon code via backend.

Steps : Login to backend -> Menu -> Marketing -> Cart Price Rule -> Add New Rule

Under that Form you will find conditions section, Under That section you can add conditions based on your criteria.

You will see Attribute Drop-down Under Condition section.

If your Attribute not there then, you need to follow step,

Backend Login -> Menu -> Stores -> Products (Attribute section) -> search your attribute - > Click on that Row

Under that page you will find storefront properties, in that change the value of Use for Promo Rule Conditions to YES .

Run php bin/magento c:f command via Terminal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.