Gift certificate on payment page only.

To create a childview under the OrderWizardModuleShowShipments.

_.extend(OrderWizardModuleShowShipments.prototype, {

                    template: jj_shipping_billing_tpl,

                    childViews: _.extend({}, OrderWizardModuleShowShipments.prototype.childViews, {

                        ‘GiftCertificates’: function () {

                            return new OrderWizardModulePaymentMethodGiftCertificate({

                                model: this.model,

                                application: this.application,

                                wizard: this.wizard,

                            });

                        }

                    })

                });

_.extend(OrderWizardModulePaymentMethodGiftCertificate.prototype, {

                    applyGiftCertificate: function (e) {

                        try {

                            e.preventDefault();

                            const code = String(jQuery(e.target).find(‘[name=”code”]’).val()).trim();

                            const is_applied = _.find(this.giftCertificates, function (certificate) {

                                return certificate.get(‘giftcertificate’).code === code;

                            });

                            if (!code) {

                                this.showError({

                                    errorCode: ‘ERR_WS_EMPTY_GIFTCERTIFICATE’,

                                    errorMessage: Utils.translate(‘Gift Certificate is empty’)

                                })

                            } else if (is_applied) {

                                this.showError({

                                    errorCode: ‘ERR_WS_APPLIED_GIFTCERTIFICATE’,

                                    errorMessage: Utils.translate(‘Gift Certificate is applied’)

                                });

                            } else {

                                this.updateGiftCertificates(this.getGiftCertificatesCodes().concat(code));

                            }

                        } catch (error) {

                            console.log(‘Error’, error)

                        }

                    },

                    showError: function (error) {

                        console.error(‘Error in Gift Certificate:’, error);

                        if (typeof error === ‘string’) {

                            error = { errorMessage: error };

                        } else if (!error || typeof error !== ‘object’) {

                            error = {

                                errorMessage: Utils.translate(‘An error occurred while applying the gift certificate.’),

                                errorCode: ‘ERR_WS_EMPTY_GIFTCERTIFICATE’

                            };

                        }

                        this.error = error;

                        var view = new GlobalViewsMessageView({

                            message: this.error.errorMessage,

                            type: ‘error’,

                            closable: true

                        });

                        var placeholder = this.$(‘[data-type=”alert-placeholder-module”]:first’);

                        var container = placeholder.parents(‘.module-rendered:last’);

                        if (container.length) {

                            Utils.animatedScroll(container[0]);

                        }

                        placeholder.html(view.render().$el.html());

                        // Optionally clear the error afterward

                        // this.error = null;

                    },

                    updateGiftCertificates: function (codes) {

                        const self = this;

                        codes = _.map(codes, function (code) {

                            return { code: code };

                        });

                        // disable navigation buttons

                        this.wizard.getCurrentStep().disableNavButtons();

                        // disable inputs and buttons

                        this.$(‘input, button’).prop(‘disabled’, true);

                        return new Backbone.Model()

                            .save(

                                {

                                    giftcertificates: codes

                                },

                                {

                                    url: Utils.getAbsoluteUrl(‘services/LiveOrder.GiftCertificate.Service.ss’),

                                    success: function (model, attributes) {

                                        self.model.set({

                                            paymentmethods: attributes.paymentmethods,

                                            summary: attributes.summary,

                                            touchpoints: attributes.touchpoints

                                        });

                                    },

                                    error: function (model, jqXhr) {

                                        jqXhr.preventDefault = true;

                                        self.showError(JSON.parse(jqXhr.responseText));

                                    }

                                }

                            )

                            .always(function () {

                                // enable navigation buttons

                                self.wizard.getCurrentStep().enableNavButtons();

                                // enable inputs and buttons

                                self.$(‘input, button’).prop(‘disabled’, false);

                            });

                    },

                });

Leave a comment

Your email address will not be published. Required fields are marked *