Shopping Cart Logic

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn this is the logic idea of shopping cart in the mall. It is also a pleasure to share a record! Exchange and learning
Design Process for adding an electronic mall to a shopping cart
1. determine whether the user logs on. If the user logs on to products stored in the shopping cart using Db (that is, the user saves the items in the shopping cart using the database method), if the user does not log on (that is, the user saves the items in the shopping cart using the Cookie method, and uses encryption)
2. When you click the Buy button, you may buy a single item or a combo package. In this way, the parameter transmitted by clicking the button is either the product id or the combination package id with the product quantity.
3. for a single item, first determine whether the product is in the promotion time at this time, so the id must first determine whether it is a promotional product, update the product price, then determine whether it is a limited-time discount, and then update the product price again, finally, check whether the inventory is empty and whether the purchased quantity is greater than the inventory. For some virtual products, such as the F code and pre-sale products, it is best not to add them to the shopping cart (because these products can be directly delivered, or it may take some time to deliver the goods, for such products, we recommend that you purchase them directly. Do not add them to the shopping cart ).
4. for a combination package, you need to determine whether the product is in the combination package at this time, determine whether each product in the combination package has inventory, and calculate the total price of the combination package, when adding a package to the shopping cart, you can only put one item in the combo package in the shopping cart. At the same time, you can put the combo package id in the shopping cart page to query other items in the combo package.
Instance: /**
* Add a shopping cart
*/
Public function add (){
$ Model_goods = M ('goods ');
$ Logic_buy = D ('buy', 'lock ');
If (is_numeric ($ _ GET ['goods _ id']) {

// Add the item to the shopping cart (default)
$ Goods_id = intval ($ _ GET ['goods _ id']); // item id
$ Num = intval ($ _ GET ['num']); // number of items
If ($ goods_id <= 0) return;
// Obtain the basic information of the product by id, such as name, price, and attribute (the database of each mall is roughly the same)
$ Goods_info = $ model_goods-> getGoodsInfoById ($ goods_id );
// Flash sale. Here, if the product is in the flash sale activity and the price of the product is updated, the pointer transmission method is used, so no variable is returned.
$ Logic_buy-> getGroupbuyInfo ($ goods_info );

// Time-limited discount. for time-limited discount activities, the price of the product is also updated.
$ Logic_buy-> getXianshiInfo ($ goods_info, $ quantity );
// Check whether the stock is sufficient, whether there is a snapshot, whether it is a virtual product or a pre-sale product.
$ This-> _ check_goods ($ goods_info, $ num );

} Elseif (is_numeric ($ _ GET ['bid']) {// the ID of the combo package is entered here.

// Add the discount set to the shopping cart (single set)
If (! $ _ SESSION ['Member _ id']) {
$ This-> error ('select logon '); exit ();
}
$ Bid = intval ($ _ GET ['bid']);
If ($ bid <= 0) return;
$ Model_bl = M ('bundling ');
$ Bl_info = $ model_bl-> getBundlingInfo (array ('bid' => $ bid); // you can check the package ID and package information, whether the activity is enabled
If (empty ($ bl_info) | $ bl_info ['bl _ state'] = '0 '){
$ This-> error ('this discount package does not exist. We recommend that you purchase it separately ');
}

// Check whether each item meets the conditions and recalculate the total package price
$ Bl_goods_list = $ model_bl-> getBundlingGoodsList (array ('bl _ id' => $ bl_id ));
$ Goods_id_array = array ();
$ Bl_amount = 0;
Foreach ($ bl_goods_list as $ goods ){
$ Goods_id_array [] = $ goods ['goods _ id'];
$ Bl_amount + = $ goods ['bl _ goods_price '];
}
$ Model_goods = M ('goods ');
$ Goods_list = $ model_goods-> getGoodsInfoById ($ goods_id_array );
Foreach ($ goods_list as $ goods ){
$ This-> _ check_goods ($ goods, 1 );
}

// The discount set is inserted into the shopping cart as a record, and the picture is taken as the first product diagram in the Set
$ Goods_info = array ();
$ Goods_info ['goods _ id'] = $ goods_list [0] ['goods _ id'];
$ Goods_info ['goods _ name'] = $ bl_info ['bl _ name'];
$ Goods_info ['goods _ price'] = $ bl_amount;
$ Goods_info ['goods _ num'] = 1;
$ Goods_info ['goods _ image'] = $ goods_list [0] ['goods _ image'];
$ Goods_info ['store _ name'] = $ bl_info ['store _ name'];
$ Goods_info ['bl _ id'] = $ bid;
$ Num = 1; // The quantity of each item in the combo package is only one
}

// The logged-on status is saved to the database. When not logged-on, the COOKIE is saved.
If ($ _ SESSION ['Member _ id']) {
$ Save_type = 'db ';
$ Goods_info ['buyer _ id'] = $ _ SESSION ['Member _ id'];
} Else {
$ Save_type = 'cookies ';
}
$ Model_cart = M ('cart ');
$ Insert = $ model_cart-> addCart ($ goods_info, $ save_type, $ num );
If ($ insert ){
// The number of items in the shopping cart is recorded in the cookie.
SetNcCookie ('cart _ goods_num ', $ model_cart-> cart_goods_num, 2*3600 );
$ Data = array ('state' => 'true', 'num' => $ model_cart-> cart_goods_num, 'amount '=> ncPriceFormat ($ model_cart-> cart_all_price ));
} Else {
$ Data = array ('state' => 'false ');
}
Exit (json_encode ($ data ));
}

/**
* Check whether the product meets the conditions for adding to the shopping cart.
* @ Param unknown $ goods
* @ Param number $ quantity
*/
Private function _ check_goods ($ goods_info, $ num ){
If (empty ($ num )){
Exit (json_encode (array ('msg '=> 'item quantity error ')));
}
If (empty ($ goods_info )){
Exit (json_encode (array ('msg '=> 'item does not exist ')));
}
If (intval ($ goods_info ['goods _ store']) <1 ){
Exit (json_encode (array ('msg '=> 'item inventory insufficiency ')));
}
If (intval ($ goods_info ['goods _ store']) <$ num ){
Exit (json_encode (array ('msg '=> 'quantity purchased exceeds stocks ')));
}
If ($ goods_info ['is _ virtual '] | $ goods_info ['is _ fcode'] | $ goods_info ['is _ presell']) {
Exit (json_encode (array ('msg '=>' This item cannot be added to the shopping cart. Please buy it directly ', 'utf-8 ')));
}
}
Add two functions to function. php.
/**
* Price formatting
*
* @ Param int $ price
* @ Return string $ price_format
*/
Function ncPriceFormat ($ price ){
$ Price_format = number_format ($ price, 2 ,'.','');
Return $ price_format;
}
/**
* Set cookie
* This encryption method was written in my previous article.
* @ Param string $ name cookie name
* @ Param string $ value cookie value
* @ Param int $ expire cookie Validity Period
* @ Param string $ path the default server path for cookie is/
* @ Param string $ domain cookie domain Name
* @ Param string $ whether secure transmits cookies through secure HTTPS connections. The default value is false.
*/
Function setNcCookie ($ name, $ value, $ expire = '000000', $ path = '', $ domain ='', $ secure = false ){
If (empty ($ path) $ path = '/';
If (empty ($ domain) $ domain = SUBDOMAIN_SUFFIX? SUBDOMAIN_SUFFIX :'';
$ Name = defined ('cookie _ pre ')? COOKIE_PRE. $ name: strtoupper (substr (md5 (MD5_KEY), 0, 4). '_'. $ name;
$ Expire = intval ($ expire )? Intval ($ expire) :( intval (SESSION_EXPIRE )? Intval (SESSION_EXPIRE): 3600 );
$ Result = setcookie ($ name, $ value, time () + $ expire, $ path, $ domain, $ secure );
$ _ COOKIE [$ name] = $ value;
}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.