/** * Add items and quantities to cart cookie to return the total number of current items in the cookie*/ functionaddtoshoppingcar (ID, num, type) {var_num = 1; if(num! =undefined) {_num=num; } if(Type = =undefined) {Alert ("Please set Product type"); return false; } varTotalnum = _num;//Total number defaults to incoming parameters varCookieset = {expires:7, path: '/'};//Setting the cookie path //$.cookie (Cookieproductid, NULL, cookieset);//Clear Cookies //var jsonstr = "[{' ProductID ': '" + ID + "', ' Num ': '" + _num + "'}]"; Constructs a JSON string, ID is the item ID num is the number of this product varJsonstr = "[{' ProductID ': '" + ID + "', ' Num ': '" + _num + "', ' type ': '" + Type + "'}]";//constructs a JSON string, ID is the item ID num is the number of this productConsole.log (JSONSTR); Console.log ($.cookie (Cookieproductid)); if($.cookie (cookieproductid) = =NULL) {$.cookie (Cookieproductid, Jsonstr, cookieset);//If you don't have this cookie, set up his // ============ varJsonobj = eval (' (' + ' + $.cookie (cookieproductid) + ') ');//if so, convert the JSON string to an object varFindproduct =false;//whether to find the product ID, or True if it is found, otherwise falsh for(varObjinchjsonobj) { if(Jsonobj[obj]. ProductID = =ID) {Console.log ("Quantity:" +parseint (Jsonobj[obj]. Num)); Jsonobj[obj]. Num=parseint (Jsonobj[obj]. Num); Totalnum=Jsonobj[obj]. Num; Findproduct=true; Break; } } if(Findproduct = =false){//not found, then addJsonobj[jsonobj.length] =NewObject (); Jsonobj[jsonobj.length-1]. ProductID =ID; Jsonobj[jsonobj.length-1]. Num =num; Jsonobj[jsonobj.length-1]. Type =type; } $.cookie (Cookieproductid, Json.stringify (jsonobj), cookieset); //writing Coockie JSON requires json2.js support // ============}Else{ varJsonobj = eval ("(" + $.cookie (Cookieproductid) + ")");//if so, convert the JSON string to an object varFindproduct =false;//whether to find the product ID, or True if it is found, otherwise falsh for(varObjinchjsonobj) { if(Jsonobj[obj]. ProductID = =ID) {Console.log ("Quantity:" +parseint (Jsonobj[obj]. Num)); Jsonobj[obj]. Num= parseint (Jsonobj[obj]. Num) +_num; Totalnum=Jsonobj[obj]. Num; Findproduct=true; Break; } } if(Findproduct = =false){//not found, then addJsonobj[jsonobj.length] =NewObject (); Jsonobj[jsonobj.length-1]. ProductID =ID; Jsonobj[jsonobj.length-1]. Num =num; Jsonobj[jsonobj.length-1]. Type =type; } $.cookie (Cookieproductid, Json.stringify (jsonobj), cookieset); //writing Coockie JSON requires json2.js support } returnTotalnum; //alert ($.cookie (Cookieproductid));}
This plugin is used here for $.cookie. The code for this plugin is as follows:
/** * Creates a cookie with the given name and value and other optional parameters. * * @example $. Cookies (' The_cookie ', ' the_value '); * @desc Set the value of the cookie. * @example $. Cookie (' The_cookie ', ' the_value ', {expiry: 7, Path: '/', Domain name: ' jquery.com ', Security: true}); * @desc Create a cookie with all available options. * @example $. Cookie (' The_cookie ', ' the_value '); * @desc Create a session cookie. * @example $. Cookie (' The_cookie ', NULL); * @desc A cookie that is deleted by an empty pass value. * * @param the name of the cookie for the parameter string name. * @param the value of the cookie for the parameter string value. * @param the Selection object text of the Parameter object contains a key/value pair that provides an optional cookie attribute. * @option Number | Date Expires An integer that specifies the expiration date from now on the day or Date object. * If a negative value is specified (for example, in the past date), the cookie will be deleted. * If set to NULL or omitted, the cookie will be a session cookie and will not be retained * when the browser exits. * @option the cookie Path property of the string path attribute additional damage value (default: The cookie created by the path of the page). * @option The value of the cookie domain property of the String field (default: The cookie created by the page's domain name). * @option Boolean security, if true, the security attributes of the cookie will be set and the transfer of the cookie will * require a security protocol (such as HTTPS). * @type undefined * * @name $. Cookie * @cat Plugin/cookie * @author Klaus Hartl/[email protected] *//** Get The value of a cookie with the given name. Gets the value of the cookie for the given name. * * @example $.cookie (' The_cookie '); * @desc Get The value of a cookie. Gets the value of the cookie. * * @param String Name The name of the cookie. * @return The value of the cookie. * @type String * * @name $.cookie * @cat Plugins/cookie * @author Klaus hartl/[email protected]*/Jquery.cookie=function(name, value, options) {if(typeofValue! = ' undefined ') {//name and value given, set cookieoptions = Options | | {}; if(Value = = =NULL) {Value= ' '; Options.expires=-1; } varexpires = ' '; if(Options.expires && (typeofOptions.expires = = ' Number ' | |options.expires.toUTCString)) {vardate; if(typeofOptions.expires = = ' Number ') {Date=NewDate (); Date.settime (Date.gettime ()+ (Options.expires * 24 * 60 * 60 * 1000)); } Else{Date=Options.expires; } Expires= '; Expires= ' + date.toutcstring ();//Use expires attribute, Max-age isn't supported by IE } varPath = Options.path? ‘; Path= ' + options.path: '; varDomain = Options.domain? ‘; Domain= ' + options.domain: '; varSecure = options.secure? ‘; Secure ': '; Document.cookie= [Name, ' = ', encodeURIComponent (value), expires, path, domain, Secure].join ("); } Else{//Only name given, get cookie varCookievalue =NULL; if(Document.cookie && Document.cookie! = ") { varcookies = Document.cookie.split ('; ')); for(vari = 0; i < cookies.length; i++) { varCookie =Jquery.trim (Cookies[i]); //Does This cookie, string begin with the name we want? if(cookie.substring (0, name.length + 1) = = (name + ' = ')) ) {Cookievalue= decodeURIComponent (cookie.substring (name.length + 1)); Break; } } } returnCookievalue; }};
jquery method for shopping cart based on JSON and cookie