Home > Backend Development > PHP Tutorial > An example of php online mall shopping cart code

An example of php online mall shopping cart code

WBOY
Release: 2016-07-25 08:59:35
Original
1779 people have browsed it
  1. /**
  2. * Shopping cart shopping method
  3. * @param string $cart_id Shopping cart ID
  4. */
  5. public function __construct()
  6. {
  7. Zend_Session::start();
  8. $this->_session = new Zend_Session_Namespace('ShopCart');
  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. }
  16. ?>
Copy code

The third point, Add an item to your cart. Adding 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, the first action:

The code is as follows:

  1. /**
  2. * Add product
  3. */
  4. public function goodsAddAction()
  5. {
  6. // Add goods 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');//Quantity 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 goods 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);
  16. }
  17. ?> ;
Copy code

Line 15 of the above code:

  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. //Get 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 items in the shopping cart Total amount
  15. $this->view->total_amount = $cartB->getTotalAmount();
  16. }
  17. ?>
Copy code

Among the above codes, the first and second The actions must be separated, allowing you to click the shopping cart directly without adding products. That’s it, let’s throw some ideas into the discussion, I hope it can inspire you a little bit, I’ll be satisfied with this.



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