Show Standard or express shipping methods based on the inventory item records in checkout.

Description: We can customize the shipping methods to show when the express inventory item is present in the order; if it is not present, we can show the standard shipping method using this solution.

Dependency: custitem_jj_express_shipping // field id for determining if an item is an express shipping item or not.

SuiteScript:

define('JJ.Express.Shipping'
, [
  'LiveOrder.Model',
  'Utils',
  'underscore'
  ]
, function (
  LiveOrderModel,
  Utils,
  _
  )
{
  'use strict';
  _.extend(LiveOrderModel, {
    // @method getShipMethods
    // @param {Array<String>} order_fields
    // @returns {Array<OrderShipMethod>}
    getShipMethods: function getShipMethods(order_fields) {
      try {
        var shipmethods = _.map(order_fields.shipmethods, function (shipmethod) {
          console.log('shipmethod11', JSON.stringify(shipmethod));
          var  rate = Utils.toCurrency(shipmethod.rate.replace(/^D+/g, '')) || 0;
          return {
            internalid: shipmethod.shipmethod,
            name: shipmethod.name,
            shipcarrier: shipmethod.shipcarrier,
            rate: rate,
            rate_formatted: shipmethod.rate
          };
        });
        var hasExpressItem = this.getExpressShip(order_fields);
        if (!hasExpressItem) {
          shipmethods = _.filter(shipmethods, function (method) {
            return method.internalid !== '8389'; //Express shipping Id
          })
        }
        return shipmethods;
      } catch (error) {
        console.error('Error @ getShipMethods', error);
      }
    },
    getExpressShip: function getExpressShip(order_fields){
      try {
        var items = this.getLines(order_fields);
        var hasExpressItem = false;
        _.each(items, function (item) {
          //custitem_jj_express_shipping is the ceckbox field id which is used to determine it contains express ship item.
          if (item.item && item.item.custitem_jj_express_shipping) {
            hasExpressItem = true;
          }
        })
        return hasExpressItem;
      } catch (error) {
        console.error('Error @ getExpressShip fun', error);
      }
    }
  })
});

Leave a comment

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