1

I have the following code which is working fine. I want to update coupon Name, Description and Amount based on a coupon code name change.

This is my php file

use Magento\Checkout\Model\ConfigProviderInterface;

class CheckoutCouponDescriptionProvider implements ConfigProviderInterface
{
    protected $couponDescription;
    protected $couponName;

    public function __construct(
        \CompanyName\ModuleName\Block\Coupon $couponDescription,
        \Magento\Checkout\Block\Cart\Coupon $couponName
    ){
        $this->couponDescription = $couponDescription;
        $this->couponName = $couponName;
    }
    public function getConfig()
    {

        $config = [];
        $config['couponCouponName'] = $this->couponName->getCouponCode();
        $config['couponDiscountAmount'] = $this->couponDescription->getDiscountAmount();
        $config['couponDiscountDescription'] = $this->couponDescription->getDiscountDescription();

        return $config;
    }
}

and this is my discount.js file

define(
    [
        'jquery',
        'ko',
        'uiComponent',
        'Magento_Checkout/js/model/quote',
        'Magento_SalesRule/js/action/set-coupon-code',
        'Magento_SalesRule/js/action/cancel-coupon'
    ],
    function ($, ko, Component, quote, setCouponCodeAction, cancelCouponAction) {
        'use strict';

        var totals = quote.getTotals(),
            couponCode = ko.observable(null),
            isApplied,
            couponCouponName = ko.observable(null),
            couponDiscountAmount = ko.observable(null),
            couponDiscountDescription = ko.observable(null);

        if (totals()) {
            couponCode(totals()['coupon_code']);
        }

        isApplied = ko.observable(couponCode() != null);

        return Component.extend({

            initialize: function () {
                this._super();
                this.couponCouponName(window.checkoutConfig.couponCouponName);
                this.couponDiscountAmount(window.checkoutConfig.couponDiscountAmount);
                this.couponDiscountDescription(window.checkoutConfig.couponDiscountDescription);
                },

            defaults: {
                template: 'Magento_SalesRule/payment/discount'
            },
            couponCode: couponCode,
            couponCouponName: couponCouponName,
            couponDiscountAmount: couponDiscountAmount,
            couponDiscountDescription: couponDiscountDescription,


            /**
             * Applied flag
             */
            isApplied: isApplied,

            /**
             * Coupon code application procedure
             */
            apply: function() {
                if (this.validate()) {
                    setCouponCodeAction(couponCode(), isApplied);
                    console.log(couponCouponName());

                }
            },

            /**
             * Cancel using coupon
             */
            cancel: function() {
                if (this.validate()) {
                    couponCode('');
                    cancelCouponAction(isApplied);
                    console.log(couponDiscountDescription());
                }
            },

            /**
             * Coupon form validation
             *
             * @returns {Boolean}
             */
            validate: function () {
                var form = '#discount-form';

                return $(form).validation() && $(form).validation('isValid');
            }
        });
    }
);

edited few lines in .js file so it's more correct

Your Answer

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

Browse other questions tagged or ask your own question.