Magento 2: How to Apply Coupon Code based on Product Customizable Option












4















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




E.g.



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.










share|improve this question





























    4















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




    E.g.



    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.










    share|improve this question



























      4












      4








      4


      3






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




      E.g.



      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.










      share|improve this question
















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




      E.g.



      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.







      magento2 custom-options coupon coupon-codes promotions






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 7 at 10:02







      Ashish Viradiya

















      asked Jan 4 at 5:45









      Ashish ViradiyaAshish Viradiya

      745622




      745622






















          2 Answers
          2






          active

          oldest

          votes


















          3














          The solution is to use a plugin on MagentoQuoteModelCouponManagement 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="MagentoQuoteModelCouponManagement">
          <plugin name="checkout_coupon_apply"
          type="VendorModulePluginCheckoutCouponApply"/>
          </type>
          </config>


          Create plugin:



          <?php 

          namespace VendorModulePlugin;

          class CheckoutCouponApply {

          /**
          * Quote repository.
          *
          * @var MagentoQuoteApiCartRepositoryInterface
          */
          protected $quoteRepository;

          /**
          * Constructs a coupon read service object.
          *
          * @param MagentoQuoteApiCartRepositoryInterface $quoteRepository Quote repository.
          */
          public function __construct(
          MagentoQuoteApiCartRepositoryInterface $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'));
          }

          }
          }





          share|improve this answer

































            0














            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.






            share|improve this answer























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "479"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f256698%2fmagento-2-how-to-apply-coupon-code-based-on-product-customizable-option%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3














              The solution is to use a plugin on MagentoQuoteModelCouponManagement 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="MagentoQuoteModelCouponManagement">
              <plugin name="checkout_coupon_apply"
              type="VendorModulePluginCheckoutCouponApply"/>
              </type>
              </config>


              Create plugin:



              <?php 

              namespace VendorModulePlugin;

              class CheckoutCouponApply {

              /**
              * Quote repository.
              *
              * @var MagentoQuoteApiCartRepositoryInterface
              */
              protected $quoteRepository;

              /**
              * Constructs a coupon read service object.
              *
              * @param MagentoQuoteApiCartRepositoryInterface $quoteRepository Quote repository.
              */
              public function __construct(
              MagentoQuoteApiCartRepositoryInterface $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'));
              }

              }
              }





              share|improve this answer






























                3














                The solution is to use a plugin on MagentoQuoteModelCouponManagement 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="MagentoQuoteModelCouponManagement">
                <plugin name="checkout_coupon_apply"
                type="VendorModulePluginCheckoutCouponApply"/>
                </type>
                </config>


                Create plugin:



                <?php 

                namespace VendorModulePlugin;

                class CheckoutCouponApply {

                /**
                * Quote repository.
                *
                * @var MagentoQuoteApiCartRepositoryInterface
                */
                protected $quoteRepository;

                /**
                * Constructs a coupon read service object.
                *
                * @param MagentoQuoteApiCartRepositoryInterface $quoteRepository Quote repository.
                */
                public function __construct(
                MagentoQuoteApiCartRepositoryInterface $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'));
                }

                }
                }





                share|improve this answer




























                  3












                  3








                  3







                  The solution is to use a plugin on MagentoQuoteModelCouponManagement 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="MagentoQuoteModelCouponManagement">
                  <plugin name="checkout_coupon_apply"
                  type="VendorModulePluginCheckoutCouponApply"/>
                  </type>
                  </config>


                  Create plugin:



                  <?php 

                  namespace VendorModulePlugin;

                  class CheckoutCouponApply {

                  /**
                  * Quote repository.
                  *
                  * @var MagentoQuoteApiCartRepositoryInterface
                  */
                  protected $quoteRepository;

                  /**
                  * Constructs a coupon read service object.
                  *
                  * @param MagentoQuoteApiCartRepositoryInterface $quoteRepository Quote repository.
                  */
                  public function __construct(
                  MagentoQuoteApiCartRepositoryInterface $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'));
                  }

                  }
                  }





                  share|improve this answer















                  The solution is to use a plugin on MagentoQuoteModelCouponManagement 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="MagentoQuoteModelCouponManagement">
                  <plugin name="checkout_coupon_apply"
                  type="VendorModulePluginCheckoutCouponApply"/>
                  </type>
                  </config>


                  Create plugin:



                  <?php 

                  namespace VendorModulePlugin;

                  class CheckoutCouponApply {

                  /**
                  * Quote repository.
                  *
                  * @var MagentoQuoteApiCartRepositoryInterface
                  */
                  protected $quoteRepository;

                  /**
                  * Constructs a coupon read service object.
                  *
                  * @param MagentoQuoteApiCartRepositoryInterface $quoteRepository Quote repository.
                  */
                  public function __construct(
                  MagentoQuoteApiCartRepositoryInterface $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'));
                  }

                  }
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 16 mins ago

























                  answered Jan 4 at 7:10









                  Ashish ViradiyaAshish Viradiya

                  745622




                  745622

























                      0














                      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.






                      share|improve this answer




























                        0














                        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.






                        share|improve this answer


























                          0












                          0








                          0







                          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.






                          share|improve this answer













                          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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 4 at 6:02









                          Nauman m. MansuriNauman m. Mansuri

                          19618




                          19618






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Magento Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f256698%2fmagento-2-how-to-apply-coupon-code-based-on-product-customizable-option%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              What other Star Trek series did the main TNG cast show up in?

                              Berlina muro

                              Berlina aerponto