An example of the code of Shopping cart in PHP online Mall

Source: Internet
Author: User
Tags php online
    1. /**
    2. * Shopping Cart Type 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

3rd, add a product to the shopping cart. Add a product to the shopping cart, where I think of it as a two action. First action: Add items to the shopping cart database. Second action: Find all items in the shopping cart and show them. First is the first action:

The code is as follows:

    1. /**
    2. * Add Item
    3. */
    4. Public Function goodsaddaction ()
    5. {
    6. Add a product using GET request
    7. $goods _id = $this->_getparam (' goods_id ');//commodity ID
    8. $goods _spec = $this->_getparam (' filter_name ');//commodity properties (color, size)
    9. $goods _number = $this->_getparam (' goods_number ');//Quantity of goods
    10. $promote _name = $this->_getparam (' promote_name ', ' Default ');//Promotion Strategy
    11. Get a shopping cart instance
    12. $cartB = $this->_getcart ();
    13. $cartB->goodsadd ($goods _id, $goods _spec, $goods _number, $promote _name);
    14. Add success, jump to Next, find all items in cart and show them out.
    15. $this->_showmessage (Bll_context::iserror ()? Bll_context::geterror (): ' Add to basket success! ', Bll_context::getrecirect ('/orderv2 '), 3);
    16. }
    17. ?>
Copy Code

Line 15th of the previous code:

    1. /**
    2. * Shopping List
    3. */
    4. Public Function indexaction ()
    5. {
    6. Get a shopping cart instance
    7. $cartB = $this->_getcart ();
    8. List all items in your shopping cart
    9. $this->view->goods_list = $cartB->goodsviewlist ();
    10. Gets a 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 merchandise in your shopping cart
    15. $this->view->total_amount = $cartB->gettotalamount ();
    16. }
    17. ?>
Copy Code

In the above code, the first and second actions must be separate, allowing no items to be added directly to the shopping cart. That's all, let's hope that we have a little bit of inspiration, in the next satisfaction.

  • 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.