jQuery数组-$inArray
如果购物车中有多个SKU,我会抛出一条错误消息,但我需要对我的规则做一个例外。下面是我的代码 基本上,这里的情况是,JNL758是唯一一个也可以与CustomizedProductId和customizedProductIDs2中的任何SKU一起出现在购物车中的SKU 如果购物车中有来自customizedProductIDs或customizedProductIDs2以及JNL758的1个SKU,则我需要一些规则,该规则不会显示错误消息,但这样就可以了-用户不能在购物车中有其他SKUjQuery数组-$inArray,jquery,arrays,Jquery,Arrays,如果购物车中有多个SKU,我会抛出一条错误消息,但我需要对我的规则做一个例外。下面是我的代码 基本上,这里的情况是,JNL758是唯一一个也可以与CustomizedProductId和customizedProductIDs2中的任何SKU一起出现在购物车中的SKU 如果购物车中有来自customizedProductIDs或customizedProductIDs2以及JNL758的1个SKU,则我需要一些规则,该规则不会显示错误消息,但这样就可以了-用户不能在购物车中有其他SKU var
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;
}
}
假设@JNL0500、JNL758和JNL123在购物车中-我需要显示错误消息。如果购物车中只有@JNL0500和JNL758,则不会显示任何错误消息
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;
}
}
var customizedProductId=[“@JNL0500”、“@JNL0501”、“@JNL0502”、“@JNL0503”、“@JNL0504”];
var customizedProductIDs2=[“@JNL0408”、“@JNL1036”、“@JNL1735”、“JNL1005”、“@JNL0222”、“@JNL0221”];
var cardsku=[“JNL758”];
函数cartItemCountError(){
如果($451&&$451.OrderDetails&&$451.OrderDetails.lineItems){
var cartItemCount=$451.OrderDetails.lineItems.length;
var错误=错误;
如果(cartItemCount>1){
对于(变量i=0;i
我认为最简单的处理方法可能是从要测试的值数组中删除cardsku的“例外”项,如图所示,然后继续测试规则 cardSKU的存在会改变您需要检查的阈值,该阈值定义购物车是否有效
一些伪代码以显示更简单的形式
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
}