Php uses session to implement the simplest way to implement the shopping cart class-PHP source code

Source: Internet
Author: User
Ec (2); classcart {& nbsp; ** & nbsp; * & nbsp; function name: & nbsp; & nbsp; addItem & nbsp; * & nbsp; function: & nbsp; add product & nbsp; * & nbsp; input parameter: script ec (2); script

Class cart
{
/**
* Function name: addItem
* Function: Add a product
* Input parameter: $ data ------------- item Array
* Function return value: none
* Other Instructions: because the data is recorded in the session, no response is required.
*/
Function addItem ($ data ){
Global $ config_kezhi;
If (is_array ($ data )&&! Empty ($ data )){
Foreach ($ data as $ key => $ val ){
// Add quantity and price if the product exists
If ($ this-> _ isExists ($ key )){

$ _ SESSION ['cart'] [$ key] ["paddy_count"] + = $ val ['Paddy _ count'];
$ _ SESSION ['cart'] [$ key] ["market_count"] + = $ val ['market _ count'];
$ _ SESSION ['cart'] [$ key] ["goodsnum"] + = intval ($ val ['goodsnum']);
$ Arr = $ this-> getItems ($ key );
// $ _ SESSION ['cart'] [$ key] ["count"] + = $ _ SESSION ['cart'] [$ key] ["price"];
/* If (strlen ($ _ SESSION ['cart'] [$ key] ["kezhi"])> 0) {// no lettering added
$ _ SESSION ['cart'] [$ key] ["eng_price"] = 0;
$ _ SESSION ['cart'] [$ key] ["eng_price"] = (int) $ config_kezhi * intval ($ arr ['num']);
} Else {
// Die ("dd ");
$ _ SESSION ['cart'] [$ key] ["eng_price"] = 0;
}*/
// Echo $ arr ['num'];
// Die ();

}
// Otherwise, join directly
Else
{
$ _ SESSION ['cart'] [$ key] = $ data [$ key];
// $ _ SESSION ['cart'] [$ key] ["name"] = $ val ['name'];
// $ _ SESSION ['cart'] [$ key] ["price"] = $ val ['price'];
// $ _ SESSION ['cart'] [$ key] ["count"] = round ($ val ['price'] * intval ($ val ['num']), 2 );
// $ _ SESSION ['cart'] [$ key] ["num"] = intval ($ val ['num']);
}
}
}
}

/**
* Function name: _ isExists
* Function: determines whether the product exists.
* Input parameter: $ id ---------- item ID
* Function return value: bool
* Other Instructions:
*/
Function _ isExists ($ id)
{
If (isset ($ _ SESSION ['cart'] [$ id]) &! Empty ($ _ SESSION ['cart'] [$ id]) & array_key_exists ($ id, $ _ SESSION ['cart'])
{
Return true;
}
Else
{
Return false;
}
}

/**
* Function name: modItem
* Function: modifies the number of items.
* Input parameter: $ id ------------ item ID
* $ Num ------------- number of items
* Function return value: return value description
* Other Instructions: Description
*/
Function modItem ($ id, $ num)
{
$ Arr = $ this-> getItems ($ id );
// Add quantity and price if the product exists
If ($ this-> _ isExists ($ id ))
{
$ _ SESSION ['cart'] [$ id] ["goodsnum"] = intval ($ num );
If ($ arr ['Paddy _ price_special ']> "0 "){
$ _ SESSION ['cart'] [$ id] ["paddy_count"] = round ($ arr ['Paddy _ price_special '] * intval ($ num), 2 );
} Elseif ($ arr ['Paddy _ userprice']> "0 "){
$ _ SESSION ['cart'] [$ id] ["paddy_count"] = round ($ arr ['Paddy _ userprice'] * intval ($ num), 2 );
} Elseif ($ arr ['Paddy _ spreadprice']> "0 "){
$ _ SESSION ['cart'] [$ id] ["paddy_count"] = round ($ arr ['Paddy _ spreadprice'] * intval ($ num), 2 );
} Else {
$ _ SESSION ['cart'] [$ id] ["paddy_count"] = round ($ arr ['Paddy _ price'] * intval ($ num), 2 );
}
$ _ SESSION ['cart'] [$ id] ["market_count"] = round ($ arr ['market _ price'] * intval ($ num), 2 );
}
}


/**
* Function name: modItem_wanwei
* Function: Modify the product wrist circumference
* Input parameter: $ id ------------ item ID
* $ Num ------------- product wrist circumference
* Function return value: return value description
* Other Instructions: Description
*/
Function modItem_wanwei ($ id, $ wanwei)
{
$ Arr = $ this-> getItems ($ id );
// Add quantity and price if the product exists
If ($ this-> _ isExists ($ id ))
{
$ _ SESSION ['cart'] [$ id] ["wanwei"] = intval ($ wanwei );

/// $ _ SESSION ['cart'] [$ id] ["count"] = round ($ arr ['price'] * intval ($ num), 2 );
}
}

// Function modItem_kezhi ($ id, $ kezhi)
//{
// Global $ config_kezhi;
// $ Arr = $ this-> getItems ($ id );
/// Add quantity and price if the product exists
// If ($ this-> _ isExists ($ id ))
//{
// $ _ SESSION ['cart'] [$ id] ["kezhi"] = $ kezhi;
// $ _ SESSION ['cart'] [$ id] ['price'] = $ arr ['price'] + $ config_kezhi;
// $ _ SESSION ['cart'] [$ id] ['Count'] = round ($ _ SESSION ['cart'] [$ id] ['price'] * intval ($ arr ['num']), 2 );
//}
//}


Function modItem_kezhi ($ id, $ kezhi, $ kezhi1 = "", $ kezhi2 = "")
{
Global $ config_kezhi;
$ Arr = $ this-> getItems ($ id );
// Add quantity and price if the product exists
// Var_dump ($ _ SESSION ['cart']);
// Die ();
If ($ this-> _ isExists ($ id ))
{
If (strlen ($ _ SESSION ['cart'] [$ id] ["kezhi"]) = 0 & strlen ($ kezhi )! = 0) {// no lettering added
// $ Arr ['price'] + = $ config_kezhi;
$ _ SESSION ['cart'] [$ id] ["eng_price"] = 0;
$ _ SESSION ['cart'] [$ id] ["eng_price"] = (int) $ config_kezhi * intval ($ arr ['num']);
// $ _ SESSION ['cart'] [$ id] ["price"] = $ arr ['price'];
// $ _ SESSION ['cart'] [$ id] ["count"] = round ($ arr ['price'] * intval ($ arr ['num']), 2 );
$ _ SESSION ['cart'] [$ id] ["kezhi"] = $ kezhi;
$ _ SESSION ['cart'] [$ id] ["kezhi1"] = $ kezhi1;
$ _ SESSION ['cart'] [$ id] ["kezhi2"] = $ kezhi2;
/// $ _ SESSION ['cart'] [$ id] ["count"] = round ($ arr ['price'] * intval ($ num), 2 );
} Elseif (strlen ($ _ SESSION ['cart'] [$ id] ["kezhi"])! = 0 & strlen ($ kezhi) = 0) {// with lettering removed
$ _ SESSION ['cart'] [$ id] ["eng_price"] = 0;
// $ _ SESSION ['cart'] [$ id] ["eng_price"]-= (int) $ config_kezhi * intval ($ arr ['num']);
// $ _ SESSION ['cart'] [$ id] ["eng_price"]-= $ config_kezhi;
// $ _ SESSION ['cart'] [$ id] ["price"] = $ arr ['price'];
// $ _ SESSION ['cart'] [$ id] ["count"] = round ($ arr ['price'] * intval ($ arr ['num']), 2 );
$ _ SESSION ['cart'] [$ id] ["kezhi"] = $ kezhi;
$ _ SESSION ['cart'] [$ id] ["kezhi1"] = $ kezhi1;
$ _ SESSION ['cart'] [$ id] ["kezhi2"] = $ kezhi2;
} Elseif (strlen ($ _ SESSION ['cart'] [$ id] ["kezhi"])! = 0 & strlen ($ kezhi )! = 0) {// with lettering, change the lettering
// $ _ SESSION ['cart'] [$ id] ["count"] = round ($ arr ['price'] * intval ($ arr ['num']), 2 );
// $ _ SESSION ['cart'] [$ id] ["eng_price"] = 0;
$ _ SESSION ['cart'] [$ id] ["eng_price"] = (int) $ config_kezhi * intval ($ arr ['num']);

$ _ SESSION ['cart'] [$ id] ["kezhi"] = $ kezhi;
$ _ SESSION ['cart'] [$ id] ["kezhi1"] = $ kezhi1;
$ _ SESSION ['cart'] [$ id] ["kezhi2"] = $ kezhi2;
}
}
}

/**
* Function name: getItems
* Function: obtains the product array.
* Input parameter: $ id ------------- ID of a product
* Function return value: array
* Other Instructions:
*/
Function getItems ($ id = null)
{
If (isset ($ _ SESSION ['cart'])
{
If ($ id = null)
{
// Print_r ($ _ SESSION ['cart']); exit;
Return $ _ SESSION ['cart'];
}
Else
{
Return $ _ SESSION ['cart'] [$ id];
}
}
}

/**
* Function name: emptyItem
* Function: delete a product.
* Input parameter: $ id ----------- item ID
* Function return value: bool
* Other Instructions:
*/
Function emptyItem ($ id = null)
{
If ($ id = null)
{
Unset ($ _ SESSION ['cart']);
}
Else
{
Unset ($ _ SESSION ['cart'] [$ id]);
}
}

/**
* Function name: sum
* Function: calculates the total price.
* Input parameter: none
* Function return value: int
* Other Instructions:
*/
Function sum ()
{
$ Total = 0;
If (isset ($ _ SESSION ['cart']) &! Empty ($ _ SESSION ['cart'])
{
Foreach ($ _ SESSION ['cart'] as $ key => $ val)
{
$ Total + = $ val ['Count'];
}
}
Return $ total;
}
}

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.