PHP Online Mall shopping Cart design code sharing _php instance

Source: Internet
Author: User
Tags php online advantage
First, the design of the shopping Cart database:
1. Id
2. goods_id Product ID
3. session_id Shopping Cart ID
4. GOODS_SN Commodity Code
5. Goods_name Product Name
6. Shop_price Commodity Mall selling price
7. Goods_price the real price of goods (and Shop_price is the difference is that, when the discount, Shop_price is discounted before the price of goods, and Goods_price is discounted after)
8. Number of Goods_number
9. Weight Commodity weight
Goods_attr commodity properties (e.g. color, size)
Promote_code Promotional Code (for merchandise promotion, do the strategy Pattern identification code)
Is_promote Promotional ID
STOCK_ID Inventory ID
The database structure is probably like this. Of course, if there are other needs, such as merchandise rebate points, whether the product is virtual products, whether the goods on behalf of the delivery, and so on, you can add additional identifiers. Here is not an example.
2nd, our shopping cart requirements are in the user does not log in the state can add goods to the shopping cart, this is also a very popular way recently, so in the shopping cart database design inside, did not join user_id This field, considering that the user did not log in can also put the goods into the shopping cart.
So to identify which user is the shopping cart, it is necessary to use session_id, is the user corresponding to the unique shopping cart identification code. This code can be built into the constructor of cart object cart:
Copy Code code as follows:

/**
* Shopping Cart Type shopping method
* @param string $cart _id shopping Cart ID
*/
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;
}

The advantage of all things is that the advantages of this design is that you can use the shopping cart without logging in. The advantage is that two people share a computer, the shopping cart can not determine which is the product of the person.
3rd, add a product to the shopping cart.
Add a product to the shopping cart, and here I think of it as two moves.
First action: Add the item to the shopping cart database.
Second action: Find all items in the shopping cart and show them.
First is the first action:
Copy Code code as follows:

/**
* Add Product
*/
Public Function goodsaddaction ()
{
Add Item use GET request
$goods _id = $this->_getparam (' goods_id ');//Product ID
$goods _spec = $this->_getparam (' filter_name ');//commodity attributes (color, size)
$goods _number = $this->_getparam (' goods_number ');//Quantity of goods
$promote _name = $this->_getparam (' promote_name ', ' Default ')/promotion strategy
Get a shopping cart instance
$cartB = $this->_getcart ();
$cartB->goodsadd ($goods _id, $goods _spec, $goods _number, $promote _name);
Add success, jump to Next, find all merchandise for cart and show it.
$this->_showmessage (bll_context::iserror)? Bll_context::geterror (): ' Add to basket success! ', Bll_context::getrecirect ('/orderv2 '), 3);
}

Line 15th of the previous paragraph:
$cartB->goodsadd ($goods _id, $goods _spec, $goods _number, $promote _name);
This is the addition of the product operation function, which $promote_name is a promotional parameter, specifically using the strategy model to choose which promotional strategy, I am prepared to discuss the next article specifically. As long as you know it is to add the basic information of this product, stored in the shopping cart database can be.
The second action is:
Copy Code code as follows:

/**
* Shopping List
*/
Public Function indexaction ()
{
Get a shopping cart instance
$cartB = $this->_getcart ();
List all items in the shopping cart
$this->view->goods_list = $cartB->goodsviewlist ();
Gets a list of rule instances used to display rule messages
$this->view->tips = $cartB->goodstiprules ();
Total quantity of goods in shopping cart
$this->view->total_number = $cartB->gettotalgoodsnumber ();
Get the total amount of merchandise in the shopping cart
$this->view->total_amount = $cartB->gettotalamount ();
}

The first and second actions here must be separate, because the user can also click the cart without adding the item directly.

Here is the effect chart:



The first post, what is not enough, not clear, but also please forgive me. 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.