PHP online shopping cart design code sharing_PHP tutorial

WBOY
Release: 2016-07-21 15:21:17
Original
875 people have browsed it

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 is the selling price of the goods in the mall
7. goods_price is the real selling price of the goods (the difference from shop_price is that when there is a discount, shop_price is the selling price of the goods before the discount, while goods_price is after the discount)
8. goods_number quantity
9. weight product weight
10. goods_attr product attributes (such as color, size)
11. promote_code promotion code (strategy pattern identification code for product promotion)
12. is_promote Promotion ID
13. stock_id Stock ID
The database structure is roughly like this. Of course, if there are other needs, such as rebate points for the product, whether the product is a virtual product, whether the product is shipped on behalf of others, etc., you can add additional identifiers. I won’t give examples one by one here.
Second point, our shopping cart requirement is that users can add products to the shopping cart without logging in. This is also a very popular method recently, so it is not included in the shopping cart database design. The user_id field takes into account that users can put products into the shopping cart even if they are not logged in.
So to identify which user the shopping cart belongs to, you need to use session_id, which is the unique identification code of the user's corresponding shopping cart. Generating this code can be placed in the constructor of the shopping cart object Cart:

Copy code The code is as follows:

/**
* Shopping cart 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;
}

Everything has advantages and disadvantages. The advantage of this design is that you can use the shopping cart without logging in; the disadvantage is that two people share a computer, and the shopping cart cannot determine who owns the product.
The third point is to add an item to the shopping cart.
Add an item to the shopping cart. Here, I think of it as two actions.
The first action: add products to the shopping cart database.
Second action: Find all the items in the shopping cart and display them.
First is the first action:
Copy code The code is as follows:

/**
* Add product
*/
public function goodsAddAction()
{
//Add goods using get request
$goods_id = $this->_getParam('goods_id');//Goods ID
$goods_spec = $this->_getParam('filter_name');//Product attributes (color, size)
$goods_number = $this->_getParam('goods_number');//Quantity of goods
$promote_name = $ this->_getParam('promote_name', 'Default');//Promotion strategy
//Get shopping cart instance
$cartB = $this->_getCart();
$cartB-> ;goodsAdd($goods_id, $goods_spec, $goods_number, $promote_name);
//Added successfully, jump to the next step, find all the items in the shopping cart, and display them.
$this->_showMessage(Bll_Context::isError() ? Bll_Context::getError() : 'Add to shopping basket successfully!', Bll_Context::getRecirect('/orderv2'), 3);
}

Line 15 of the above code:
$cartB->goodsAdd($goods_id, $goods_spec, $goods_number, $promote_name);
This is the add product operation function, this The $promote_name inside is a parameter of promotion. Specifically, it uses strategy mode to select which promotion strategy to use. I plan to discuss it in detail in the next article. All you need to know here is to add the basic information of this product and store it in the shopping cart database.
The second step is the second action:
Copy code The 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();
//Get 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 shopping Total amount of goods in the car
$this->view->total_amount = $cartB->getTotalAmount();
}

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

The following is the rendering:



This is my first post, so please forgive me if there are any shortcomings or unclear things. Welcome to discuss!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324979.htmlTechArticleFirst, 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 product mall price 7....
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!