Mall shopping php Online shopping cart design code share

Source: Internet
Author: User
Tags php online
First, the design of the shopping Cart database:
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 Commodity Mall Price
7. Goods_price Commodity Real Price (the difference with Shop_price is that when the discount, Shop_price is the price of the product before the discount, and Goods_price is after the discount)
8. Number of Goods_number
9. Weight Product Weight
Goods_attr product attributes (e.g. color, size)
Promote_code Promo code (for merchandising, strategy pattern identification code)
Is_promote Promo ID
STOCK_ID Stock ID
The database structure is probably like this. Of course, if there are other needs, such as merchandise rebate points, whether the product is a virtual product, whether the goods on behalf of the shipment, etc., can be added to the indicator. Here is not an example.
2nd, our shopping cart requirements are in the user is not logged in the state can be added to the shopping cart, which is also a very popular way, so in the shopping cart database design, there is no user_id this field, considering that the user has not landed under the product can be placed in the shopping cart.
Then identify the shopping cart in the end is which one user, is the need to use the session_id, is the user's corresponding shopping cart unique identification code. This code can be built in the constructor of the Cart object cart:

Copy the Code code as follows:


/**
* Shopping Cart Type shopping method
* @param string $cart _id shopping Cart ID
*/
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;
}


There are pros and cons, the advantage of this design is that you can use the shopping cart without logging in, the advantage is that the two people share a computer, the shopping cart can not determine who is the product.
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:

Copy the Code code as follows:


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


Line 15th of the previous code:
$cartB->goodsadd ($goods _id, $goods _spec, $goods _number, $promote _name);
This is the addition of the product operation function, which $promote_name is a promotion of the parameters, in particular, using the strategy model to choose which promotional strategy, I am prepared to discuss the next article specifically. As long as you know it is to add the basic information of this product, deposit into the shopping cart database.
Next is the second action:

Copy the Code code as follows:


/**
* Shopping List
*/
Public Function indexaction ()
{
Get a shopping cart instance
$cartB = $this->_getcart ();
List all items in your shopping cart
$this->view->goods_list = $cartB->goodsviewlist ();
Gets a 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 merchandise in your shopping cart
$this->view->total_amount = $cartB->gettotalamount ();
}


Here the first and second action must be separate, because the user can also not add merchandise direct point shopping cart.

Here are the following:

The first post, what is insufficient, not clear, but also forgive me. Welcome to the discussion!

The above introduces the mall shopping PHP online shopping cart design code sharing, including shopping mall content, I hope that the PHP tutorial interested friends have helped.

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