Online shopping cart Design

Source: Internet
Author: User
Tags zend framework

What we need to do is a shopping cart that can contain promotions. Therefore, it is a little more complicated than other simple shopping cart. (PHP's zend framework is used)

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:

1 /**
2 * shopping methods for shopping cart
3 * @ Param string $ cart_id: ID of the shopping cart
4 */
5 public function _ construct ()
6 {
7 zend_session: Start ();
8 $ this-> _ session = new zend_session_namespace ('shoppart ');
9 If (! Isset ($ this-> _ Session-> session_id ))
10 {
11 $ this-> _ Session-> session_id = MD5 (uniqid (mt_rand (), true ));
12 $ this-> _ Session-> info = array ();
13}
14 $ this-> _ cart_id = $ this-> _ Session-> session_id;
15}

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:

1 /**
2 * add product
3 */
4 public function goodsAddAction ()
5 {
6 // Add product use get request
7 $ goods_id = $ this-> _ getParam ('goods _ id'); // item id
8 $ goods_spec = $ this-> _ getParam ('filter _ name'); // item attributes (color, size)
9 $ goods_number = $ this-> _ getParam ('goods _ number'); // number of items
10 $ promote_name = $ this-> _ getParam ('promote _ name', 'default'); // promotion policy
11
12 // get the shopping cart instance
13 $ cartB = $ this-> _ getCart ();
14
15 $ cartB-> goodsAdd ($ goods_id, $ goods_spec, $ goods_number, $ promote_name );
16 // after the item is successfully added, go to the next step, find all the items in the shopping cart, and display them.
17 $ this-> _ showMessage (Bll_Context: isError ()? Bll_Context: getError (): 'added to the shopping basket! ', Bll_Context: getRecirect ('/orderv2 '), 3 );
18}

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:

1 /**
2 * shopping list
3 */
4 public function indexAction ()
5 {
6 // get the shopping cart instance
7 $ cartB = $ this-> _ getCart ();
8 // list all items in the shopping cart
9 $ this-> view-> goods_list = $ cartB-> goodsViewList ();
10 // obtain the list of rule instances used to display rule messages
11 $ this-> view-> tips = $ cartB-> goodsTipRules ();
12 // Total number of items in the shopping cart
13 $ this-> view-> total_number = $ cartB-> getTotalGoodsNumber ();
14 // get the total amount of goods in the shopping cart
15 $ this-> view-> total_amount = $ cartB-> getTotalAmount ();
16}

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!

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.