Restricting Cart to 100 Unique Items in SCA websites

The customization modifies the addToCart method of ‘Cart.AddToCart.Button.View’ view file’s prototype using Underscore’s _.wrap function to intercept the original method. It checks the current number of unique items in the cart by accessing the LiveOrderModel and its lines collection. If the cart already contains 100 unique items and the new item isn’t already present, the code prevents the addition, displays a warning message using SweetAlert, and stops the default action. Otherwise, it proceeds with the original addToCart functionality.

var prototype = CartAddToButtonView.prototype;

 _.wrap(prototype.addToCart, function (orgRet, e) {

  var cart = LiveOrderModel.getInstance();

  var cartLines = cart.get(‘lines’).models;

  var cartLength = cartLines.length;

  var self = this;

  var newItemInternalId = self.model.get(‘item’).get(‘internalid’);

  var isItemAlreadyInCart = _.find(cartLines, function(line) {

   return line.get(‘item’).get(‘internalid’) === newItemInternalId;

  });

  if (cartLength >= 100 && !isItemAlreadyInCart) {

   if (e && typeof e.preventDefault === ‘function’) {

    e.preventDefault();

   }

   swal({

    title: “”,

    text: “Your cart currently contains 100 SKUs, which is the maximum supported per order. To purchase additional items, please create a new order.”,

    type: ‘warning’,

    confirmButtonText: ‘Close’

   });

   return false;

  }

  return orgRet.call(this,e)

 });

Leave a comment

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