Extending the CartItem and ShippingGroupRelationship view models

The CartItem and ShippingGroupRelationship view models are publicly available and you can extend them to behave in whatever way your storefront requirements demand.

To override the methods belonging to these view models, you must create an extension that uploads an application-level JavaScript module that depends on the view models. The following code samples show what the contents of these JavaScript modules might look like. The first sample shows how to extend the canAddShippingGroupRelationship method in the CartItem view model.

define(
  //-------------------------------------------------------------------
  // DEPENDENCIES
  //-------------------------------------------------------------------
  ['viewModels/cart-item'],
  //-------------------------------------------------------------------
  // Module definition
  //-------------------------------------------------------------------
  function(CartItem) {

    "use strict";

    return {
      onLoad: function() {
        CartItem.prototype.canAddShippingGroupRelationship = function () {
          // Override code goes here
        };
      }
    };
});

This sample shows how to extend the addQuantity method in the ShippingGroupRelationship view model.

define(
  //-------------------------------------------------------------------
  // DEPENDENCIES
  //-------------------------------------------------------------------
  ['viewModels/shipping-group-relationship'],
  //-------------------------------------------------------------------
  // Module definition
  //-------------------------------------------------------------------
  function(ShippingGroupRelationship) {

    "use strict";

    return {
      onLoad: function() {
        ShippingGroupRelationship.prototype.addQuantity = function (x) {
          // Override code goes here
        };
      }
    };
});

Note: For general information on creating an application-level JavaScript extension, see Understand widgets.