Php online shopping mall shopping cart code example
- /**
- * Shopping methods for shopping cart
- * @ Param string $ cart_id: ID of the shopping cart
- */
- 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;
- }
- ?>
-
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:
- /**
- * Add product
- */
- Public function goodsAddAction ()
- {
- // Add product use get request
- $ Goods_id = $ this-> _ getParam ('goods _ id'); // item id
- $ Goods_spec = $ this-> _ getParam ('filter _ name'); // item attributes (color, size)
- $ Goods_number = $ this-> _ getParam ('goods _ number'); // number of items
- $ Promote_name = $ this-> _ getParam ('promote _ name', 'default'); // promotion policy
- // Get the shopping cart instance
- $ CartB = $ this-> _ getCart ();
- $ CartB-> goodsAdd ($ goods_id, $ goods_spec, $ goods_number, $ promote_name );
- // After the product is successfully added, go to the next step to find all the items in the shopping cart and display them.
- $ This-> _ showMessage (Bll_Context: isError ()? Bll_Context: getError (): 'added to the shopping basket! ', Bll_Context: getRecirect ('/orderv2 '), 3 );
- }
- ?>
-
Line 2 of the code above:
- /**
- * 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 ();
- // Obtain 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 the total amount of goods in the shopping cart
- $ This-> view-> total_amount = $ cartB-> getTotalAmount ();
- }
- ?>
-
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. |