1

On my magento 2.4 I have a few coupon codes that should only be available to the admins via backend, which means that the customers on the frontend should not be able to use them. I was able to achieve this for the coupon block on the cart page by creating a Plugin on the \Magento\Checkout\Controller\Cart\CouponPost::execute() method. This method, however, doesnt work for the coupon block on the checkout page.

What I do on the cart page is:

  • check the coupon code entered by the user
  • if the coupon code is one of the restricted coupon codes
  • then removes the coupon from the cart and shows an "invalid code" error message to the user

How can I do the same on the checkout page? So when the user inserts the restricted coupon and clicks apply, I need to be able to not apply that coupon and send an error message instead

3
  • Have you found any solution yet? If yes then can you please post it here as the answer? Apr 6 at 7:22
  • Sorry for the late reply.. actually yes, I did it, although i don't quite remember how.. let me review my code, I'll post an answer as soon as I can
    – Lou Nik
    Apr 6 at 9:05
  • Sure @Lou Nik, it will be a great help. Thanks in Advance. Apr 6 at 9:50

1 Answer 1

0

Sorry for the late reply.. what i ended up doing is creating a mixin for the Magento_SalesRule/js/view/payment/discount. So:

  • Vendor/Module/view/frontend/requirejs-config.js
var config = {
    config: {
        mixins: {
            'Magento_SalesRule/js/view/payment/discount': {
                'Vendor_Module/js/view/payment/discount-mixin': true
            }
        }
    }
};
  • Vendor/Module/view/frontend/web/js/view/payment/discount-mixin.js
define(function() {
    'use strict';
    return function(target) {
        return target.extend({
            apply: function () {
                var tempCouponCode = JSON.stringify(this.couponCode());
                if( tempCouponCode.toLowerCase().indexOf($restrictedCouponCode) === -1 
                ) {
                        return this._super(); // calls the original method
                }
            },
        });
    }
});

Hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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