0

I am throwing an error message if more than one SKU is in a shopping cart, but I need to make one exception to my rule. Below is my code.

Basically what is going on here is that JNL758 is the only SKU that can also be in the cart along with any of the SKUs from customizedProductIDs and customizedProductIDs2.

I need some rule that won't display the error message if 1 SKU from customizedProductIDs or customizedProductIDs2 along with JNL758 is in the cart, but that would be it then - the user can't have an additional SKU in the cart.

Let's say @JNL0500, JNL758, and JNL123 is in the cart - I need to display the error message. No error message would display if only @JNL0500 and JNL758 is in the cart.

var customizedProductIDs = ["@JNL0500", "@JNL0501", "@JNL0502", "@JNL0503", "@JNL0504"];
var customizedProductIDs2 = ["@JNL0408", "@JNL1036", "@JNL1735", "JNL1005", "@JNL0222", "@JNL0221"];
var cardsku = ["JNL758"];

function cartItemCountError() {
  if ($451 && $451.OrderDetails && $451.OrderDetails.lineItems) {
      var cartItemCount = $451.OrderDetails.lineItems.length;
      var errors = false;
      if (cartItemCount > 1) {
          for (var i = 0; i < cartItemCount; i++) {
              var currItem = $451.OrderDetails.lineItems[i];
              // If cart has a customized item in it (See customizedProductIDs array.), it may not have any other item in the cart.)
              if ($.inArray(currItem.productID, customizedProductIDs) != -1) {
                  errors = true;
              }
              else if ($.inArray(currItem.productID, customizedProductIDs2) != -1) {
                  errors = true;
              }
              if ($.inArray(currItem.productID, customizedProductIDs2) && ($.inArray(currItem.productID, cardsku)) != -1) {
                  errors = false;
              }

          }
      }
      return errors;
  }
}
JD06
  • 191
  • 11

2 Answers2

0

I think perhaps the most straightforward way for you to handle this is to remove the "exceptional" item of the cardsku from the array of values you want to test as shown here and then go ahead and test your rules as usual.

Community
  • 1
  • 1
Carth
  • 2,243
  • 1
  • 14
  • 26
0

The presence of the cardSKU changes the threshold you need to check that defines if the cart is valid.

Some pseudo code to show a simpler form

var isCardSKU = Cart Contains cardsku 
var isCustomSKU = Cart Contains customizedProductIDs || Cart Contains customizedProductIDs2 

if (isCustomSKU && (isCardSKU && Cart.length > 2) || (!isCardSKU && Cart.length > 1)
{
  // Error here
}
Robert Beuligmann
  • 1,424
  • 10
  • 13