Add a checkout section to submit a custom transaction field.

JJ.CheckoutChange.CheckoutChange.js

define(

  ‘JJ.CheckoutChange.CheckoutChange’

  , [

    ‘JJ.CheckoutChange.CheckoutChange.View’,

    ‘LiveOrder.Model’,

    ‘JJ.CheckoutNotice.CheckoutChange.View’

  ]

  , function (

    CheckoutChangeView,

    LiveOrderModel,

    CheckoutNoticeView

  ) {

    ‘use strict’;

    return {

      mountToApp: function mountToApp(container) {

        // using the ‘Layout’ component we add a new child view inside the ‘Header’ existing view 

        // (there will be a DOM element with the HTML attribute data-view=”Header.Logo”)

        // more documentation of the Extensibility API in

        // https://system.netsuite.com/help/helpcenter/en_US/APIs/SuiteCommerce/Extensibility/Frontend/index.html

        /** @type {LayoutComponent} */

        var checkout = container.getComponent(‘Checkout’);

        this.opcrendered = false;

        var self = this;

        var SignatureControlled = false;

        var cartModel = LiveOrderModel.getInstance();

        var cartItems = cartModel.get(‘lines’);

        cartItems.forEach(function (item) {

          var Controlled = item.attributes.item.attributes.class;

          if (Controlled === ‘Controlled : Signature Controlled’) {

            SignatureControlled = true;

          }

        });

        if (checkout && SignatureControlled) {

          checkout.on(“afterShowContent”, function () {

            checkout.getCurrentStep().then(function (step) {

              if ((step.url == “opc” && !self.opcrendered)) {

             

                checkout.addModuleToStep({

                  step_url: ‘opc’,

                  module: {

                    id: ‘checkoutFieldsPrescriberView’,

                    index: 7,

                    classname: ‘JJ.CheckoutChange.CheckoutChange.View’

                  }

                });

                 

                self.opcrendered = true

              }

            })

          })

           

        }

      }

    };

  });

JJ.CheckoutChange.View.js

// @module JJ.CheckoutChange.CheckoutChange

define(‘JJ.CheckoutChange.CheckoutChange.View’

, [

    ‘jj_checkoutchange_checkoutchange.tpl’

    , ‘Wizard.Module’

    , ‘Utils’

    , ‘GlobalViews.Message.View’

    ,’JJ.CheckoutChange.CheckoutChange.SS2Model’

  ]

  , function (

    jj_checkoutchange_checkoutchange_tpl

    , WizardModule

    , Utils

    , GlobalViewsMessageView

    ,CheckoutChangeSS2Model

     

  ) {

    ‘use strict’;

    return WizardModule.extend({

      template: jj_checkoutchange_checkoutchange_tpl,

      // Event handlers for the new fields

      initialize: function () {

        var cart = this.options.wizard.application.getComponent(‘Cart’);

        console.log(“Updating Prescriber Name”,cart);

        this.model = new CheckoutChangeSS2Model();

         this.prescriber_name = ”;

         this.prescriber_email = ”;

         this.licensing_body = ”;

         this.licensing_number = ”;

         this.model.fetch().done(() => {

          this.data = this.model.toJSON();

          this.render();

        });

      },

      events: {

        ‘change [data-action=”prescriber-name”]’: ‘updatePrescriberName’,

        ‘change [data-action=”prescriber-email”]’: ‘updatePrescriberEmail’,

        // ‘change [data-action=”licensing-body”]’: ‘updateLicensingBody’,

        ‘change [data-action=”licensing-number”]’: ‘updateLicensingNumber’

      },

      updatePrescriberName: function (e) {

         

   

        try {

          const name = document.getElementById(‘prescriber-name’);

          const prescribername = name.value;

          console.log(“Prescriber Name:”, prescribername);

          this.prescriber_name = prescribername;

        } catch (error) {

          console.log(“err@saveValue”, error);

        }

      },

      updatePrescriberEmail: function (e) {

        try {

          const prescribeemail = document.getElementById(‘prescriber-email’);

          const prescribe_email = prescribeemail.value;

          this.prescriber_email = prescribe_email;

        } catch (error) {

          console.log(“err@saveValue”, error);

        }

      },

      updateLicensingBody: function (e) {

        try {

          const licensbody = document.getElementById(‘licensing-body’);

          const licens_body = licensbody.value;

          this.licensing_body = licens_body;

        } catch (error) {

          console.log(“err@saveValue”, error);

        }

      },

      updateLicensingNumber: function (e) {

        try {

          const licensnumber = document.getElementById(‘licensing-number’);

          const licens_numbernew = licensnumber.value;

          this.licensing_number = licens_numbernew;

        } catch (error) {

          console.log(“err@saveValue”, error);

        }

      },

      toNumberOrNull: function(raw) {

        try {

        var n = Number(raw);

        return isNaN(n) ? null : n;

        } catch (error) {

          console.log(“err@saveValue”, error);

        }

      },

      // This function will be responsible for saving the data to the Sales Order when submitting

      savePrescriberData: function () {

        var cart = this.options.wizard.application.getComponent(‘Cart’);

        var fieldMappings = [

          { id: ‘custbody_prescriber_name’, type: “string”, value: this.prescriber_name },

          { id: ‘custbody_prescriber_email’, type: “string”, value: this.prescriber_email },

          // { id: ‘custbody_licensing_body’, type: “string”, value: this.licensing_body },

          { id: ‘custbody_licensing_number’, type: “string”, value: this.licensing_number }

        ];

        _.each(fieldMappings, function (field) {

          if (field.value != null && field.value !== ”) {

            cart.setTransactionBodyField({

              fieldId: field.id,

              value: field.value,

              type: field.type

            })

          }

          else {

            console.log(‘Skipping empty field:’, field.id);

          }

        });

      },

      submit: function () {

        if (!this.prescriber_name || this.prescriber_name.trim() === ”) {

          const errorMessage = Utils.translate(‘Prescribing Physician Name is mandatory.’);

          const messageView = new GlobalViewsMessageView({

            message: errorMessage,

            type: ‘error’,

            closable: true

          });

          // Render error message in a dedicated placeholder in the template

          this.$(‘#prescriber-field-error’)

            .empty()

            .append(messageView.render().$el.html());

          $(‘html, body’).animate({

            scrollTop: this.$(‘#prescriber-field-error’).offset().top – 70

          }, 1000);

          return jQuery.Deferred().reject();

        }

        this.savePrescriberData();

        return jQuery.Deferred().resolve();

      },

      getContext: function getContext() {

        const options = this.model && this.model.get && this.model.get(‘options’) || {};

        const hasData = this.data && Object.keys(this.data).length > 0;

        const Data = hasData ? Object.values(this.data).flat() : [];

        return {

          prescriber_name: this.prescriber_name || options.custbody_prescriber_name || ”,

          prescriber_email: this.prescriber_email || options.custbody_prescriber_email || ”,

          // licensing_body: this.licensing_body || options.custbody_licensing_body || ”,

          licensing_number: this.licensing_number || options.custbody_licensing_number || ”,

          licensingbodydata: Data || []

        };

      },

    });

  });

Leave a comment

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