Php online shopping mall shopping cart code example

Source: Internet
Author: User
Tags php online
Php online shopping mall shopping cart code example

  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 ('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. ?>

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:

The code is as follows:

  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. // Get the shopping cart instance
  12. $ CartB = $ this-> _ getCart ();
  13. $ CartB-> goodsAdd ($ goods_id, $ goods_spec, $ goods_number, $ promote_name );
  14. // After the product is successfully added, go to the next step to find all the items in the shopping cart and display them.
  15. $ This-> _ showMessage (Bll_Context: isError ()? Bll_Context: getError (): 'added to the shopping basket! ', Bll_Context: getRecirect ('/orderv2 '), 3 );
  16. }
  17. ?>

Line 2 of the code above:

  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. }
  17. ?>

In the above code, the first and second actions must be separated, and you can directly click the shopping cart without adding items. That's all. I hope you will be inspired and satisfied in the future.

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.