PHP online mall shopping cart design code_PHP tutorial

WBOY
Release: 2016-07-13 10:42:28
Original
981 people have browsed it


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.

Second point, this shopping cart requirement is that the user can add items to the shopping cart without logging in. This is also a very popular method recently, so in the shopping cart database design, it is The user_id field is not added, considering 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. The generated code can be placed in the constructor of the shopping cart object Cart:

Java code
  1. /**
  2. * Shopping cart shopping method
  3. * website: www.jbxue.com
  4. * @param string $cart_id Cart ID
  5. */ 
  6. public function __construct()
  7. Zend_Session::start();
  8. $this->_session = new Zend_Session_Namespace('ShopCart');
  9. if(!isset($this->_session->session_id))
  10. $this->_session->session_id = md5(uniqid(mt_rand(), true));
  11. $this->_session->info = array();
  12. $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:

Java code
  1. /**
  2. * Add product
  3. * website: www.jbxue.com
  4. */ 
  5. public function goodsAddAction()
  6. //Add product using get request
  7. $goods_id = $this->_getParam('goods_id');//Product ID
  8. $goods_spec = $this->_getParam('filter_name');//Product attributes (color, size)
  9. $goods_number = $this->_getParam('goods_number');//Number of goods
  10. $promote_name = $this->_getParam('promote_name', 'Default');//Promotion strategy
  11. //Get shopping cart instance
  12. $cartB = $this->_getCart();
  13. $cartB->goodsAdd($goods_id, $goods_spec, $goods_number, $promote_name);
  14. //Added successfully, jump to the next step, find all the items in the shopping cart and display them.
  15. $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, here The $promote_name 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:

Java code
  1. /**
  2. * Shopping List
  3. */ 
  4. public function indexAction()
  5. //Get shopping cart instance
  6. $cartB = $this->_getCart();
  7. //List all items in the shopping cart
  8. $this->view->goods_list = $cartB->goodsViewList();
  9. //Get the list of rule instances used to display rule messages
  10. $this->view->tips = $cartB->goodsTipRules();
  11. //The total number of items in the shopping cart
  12. $this->view->total_number = $cartB->getTotalGoodsNumber();
  13. //Get the total value of the items in the shopping cart
  14. $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.

Articles you may be interested in:

php online mall shopping cart code
php shopping cart complete implementation code
php shopping cart function implementation code (entry example)
php shopping cart class implementation code (single case mode)
php Shopping cart class implementation code
php shopping cart code example
php shopping cart class implementation code

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/636690.htmlTechArticle1. 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 The selling price of goods in the mall 7. goods_price The real selling price of goods (with sho...
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!