Trying to setup some validation on the add to cart button for the dropdown selections for size and color on my simplecart shelf items on my wordpress site. I can get the alert to pop and block the adding of the item, but it is wrong because the alert pops even with the proper values selected. I have been searching this for days now and have found a couple people asking the same question with no response, so any input on how and if this is possible would be amazing.
Here is the add to cart function:
me.add = function () {
var me=this;
/* load cart values if not already loaded */
if( !me.pageIsReady ) {
me.initializeView();
me.update();
}
if( !me.isLoaded ) {
me.load();
me.update();
}
var newItem = new CartItem();
/* check to ensure arguments have been passed in */
if( !arguments || arguments.length === 0 ){
error( 'No values passed for item.');
return;
}
var argumentArray = arguments;
if( arguments[0] && typeof( arguments[0] ) != 'string' && typeof( arguments[0] ) != 'number' ){
argumentArray = arguments[0];
}
newItem.parseValuesFromArray( argumentArray );
newItem.checkQuantityAndPrice();
The code I am trying to implement:
if( item_Size = "none" ){
alert("Please select a size");
return;
}
if( item_Color = "none" ){
alert("Please select a color");
return;
}
Now Im a noob when it comes to this stuff so Im not even sure where it stores the values so it could be any setting variable name and the code could be completely wrong but none is the value of the default select option.
This is how it loads and saves the cookies:
me.load = function () {
var me = this;
/* initialize variables and items array */
me.items = {};
me.total = 0.00;
me.quantity = 0;
/* retrieve item data from cookie */
if( readCookie('simpleCart') ){
var data = unescape(readCookie('simpleCart')).split('++');
for(var x=0, xlen=data.length;x<xlen;x++){
var info = data[x].split('||');
var newItem = new CartItem();
if( newItem.parseValuesFromArray( info ) ){
newItem.checkQuantityAndPrice();
/* store the new item in the cart */
me.items[newItem.id] = newItem;
}
}
}
me.isLoaded = true;
};
/* save cart to cookie */
me.save = function () {
var dataString = "";
for( var item in this.items ){
dataString = dataString + "++" + this.items[item].print();
}
createCookie('simpleCart', dataString.substring( 2 ), 30 );
};
The full simpleCart code can be found here:simplecartjs.com
Any help would be greatly appreciated, I have tried other form validation techniques but am having trouble using anything meant for post validation because of the way simplecart stores variables. PLEASE HELP!!
Thank you for you time and efforts.
In the code you are trying to implement, you are assigning the value of “none” to the variable “item_size”. Since it will be true, the popup will show. You want to use two “=” when performing a comparison.
Your code:
Fixed:
At first glance that should fix the problem. Good luck.