First, the database design of the shopping cart:
1. id
2. goods_id product ID
3. session_id shopping cart ID
4. goods_sn product code
5. goods_name Product Name
6. shop_price
7. The real price of goods_price (the difference between shop_price and shop_price is that when the discount is made, shop_price is the price of the product before the discount, and goods_price is the price after the discount)
8. goods_number quantity
9. weight product weight
10. goods_attr product attributes (such as color and size)
11. promote_code: The Promotion Code (The rule pattern identification code for product promotion)
12. is_promote promotion ID
13. stock_id inventory ID
The database structure is like this. Of course, if there are other requirements, such as the product's rebate points, whether the product is a virtual product, whether the product is delivered on behalf of the other, you can add a new identifier. Here we will not give an example.
Second, our shopping cart requires that you can add items to the shopping cart without logging on to the user. This is also a very popular method recently. Therefore, in the shopping cart database design, the user_id field is not added, because the user can also put the item into the shopping cart without logging in.
To identify which user the shopping cart is, session_id is used, which is the unique identifier of the user's corresponding shopping cart. This code can be generated in the Cart constructor of the shopping Cart object:
Copy codeThe Code is as follows:
/**
* Shopping methods for shopping cart
* @ Param string $ cart_id: ID of the shopping cart
*/
Public function _ construct ()
{
Zend_Session: start ();
$ This-> _ session = new Zend_Session_Namespace ('shopcart ');
If (! Isset ($ this-> _ session-> session_id ))
{
$ This-> _ session-> session_id = md5 (uniqid (mt_rand (), true ));
$ This-> _ session-> info = array ();
}
$ This-> _ cart_id = $ this-> _ session-> session_id;
}
There are advantages and disadvantages in everything. The advantage of this design is that you can use the shopping cart without logging on. The disadvantage is that the two share a computer, and the shopping cart cannot determine which person the product is.
Third, add a product to the shopping cart.
Add a commodity to the shopping cart. Here, I think of it as two actions.
First Action: add the item to the shopping cart database.
The second action is to find and display all the items in the shopping cart.
The first action is:
Copy codeThe Code is as follows:
/**
* Add product
*/
Public function goodsAddAction ()
{
// Add product use get request
$ Goods_id = $ this-> _ getParam ('goods _ id'); // item id
$ Goods_spec = $ this-> _ getParam ('filter _ name'); // item attributes (color, size)
$ Goods_number = $ this-> _ getParam ('goods _ number'); // number of items
$ Promote_name = $ this-> _ getParam ('promote _ name', 'default'); // promotion policy
// Get the shopping cart instance
$ CartB = $ this-> _ getCart ();
$ CartB-> goodsAdd ($ goods_id, $ goods_spec, $ goods_number, $ promote_name );
// After the product is successfully added, go to the next step to find all the items in the shopping cart and display them.
$ This-> _ showMessage (Bll_Context: isError ()? Bll_Context: getError (): 'added to the shopping basket! ', Bll_Context: getRecirect ('/orderv2 '), 3 );
}
Line 2 of the code above:
$ CartB-> goodsAdd ($ goods_id, $ goods_spec, $ goods_number, $ promote_name );
This is the add product operation function. $ promote_name is a promotion parameter. Specifically, the promotion policy is used in the policy mode. I am going to discuss it in the next article. You only need to know the basic information for adding this item and store it in the shopping cart database.
The second action is:
Copy codeThe Code is as follows:
/**
* Shopping list
*/
Public function indexAction ()
{
// Get the shopping cart instance
$ CartB = $ this-> _ getCart ();
// List all items in the shopping cart
$ This-> view-> goods_list = $ cartB-> goodsViewList ();
// Obtain the list of rule instances used to display rule messages
$ This-> view-> tips = $ cartB-> goodsTipRules ();
// Total number of items in the shopping cart
$ This-> view-> total_number = $ cartB-> getTotalGoodsNumber ();
// Get the total amount of goods in the shopping cart
$ This-> view-> total_amount = $ cartB-> getTotalAmount ();
}
The first and second actions must be separated, because the user can directly click the shopping cart without adding the item.
Below is:
Sorry for the shortcomings in the first post. Welcome to the discussion!