PHP provides a comprehensive shopping cart class.

Source: Internet
Author: User
This article mainly introduces PHP's comprehensive shopping cart categories, including common functions such as adding, deleting, and modifying products. It is very useful. For more information, see

This article mainly introduces PHP's comprehensive shopping cart categories, including common functions such as adding, deleting, and modifying products. It is very useful. For more information, see

This article describes the PHP shopping cart class. Share it with you for your reference. The specific implementation method is as follows:

Recently, shopping cart was needed for a project, which may be frequently used. Therefore, it is encapsulated into a class for future calls, interested readers can simply modify this class and use it in their own programs.

The Code is as follows:

<? Php
/*************************************** **************************************/
/**/
/* File type: contains files. The recommended suffix is. inc */
/**/
/* File name: cart. inc */
/**/
/* Description: define a car purchase class */
/**/
/* Func list: class cart */
/**/
/* Author: bigeagle */
/**/
/**/
/*************************************** **************************************/

// Define the constant of this file
Define ("_ CART_INC _", "exists ");

/* Shopping cart */
Class TCart
{

Var $ SortCount; // number of product types
Var $ TotalCost; // total product value

Var $ Id; // the ID of each type of item (array)
Var $ Name; // the Name of each type of item (array)
Var $ Price; // Price of each type of product (array)
Var $ Discount; // discounts for items (array)
Var $ GoodPrice; // The preferential price of the product (array)
Var $ Count; // The number of items in each category (array)
Var $ MaxCount; // item limit (array)

// ****** Constructor
Function TCart ()
{
$ This-> SortCount = 0;

Session_start (); // initialize a session
Session_register ('sid ');
Session_register ('sname ');
Session_register ('sprice ');
Session_register ('sdiscount ');
Session_register ('sgoodprice ');
Session_register ('scount ');
Session_register ('smaxcount ');

$ This-> Update ();
$ This-> Calculate ();
}

// ******** Private, update the corresponding data in the class according to the session Value
Function Update ()
{
Global $ sId, $ sName, $ sPrice, $ sCount, $ sDiscount, $ sMaxCount, $ sGoodPrice;

If (! Isset ($ sId) or! Isset ($ sName) or! Isset ($ sPrice)
Or! Isset ($ sDiscount) or! Isset ($ sMaxCount)
Or! Isset ($ sGoodPrice) or! Isset ($ sCount) return;

$ This-> Id = $ sId;
$ This-> Name = $ sName;
$ This-> Price = $ sPrice;
$ This-> Count = $ sCount;
$ This-> Discount = $ sDiscount;
$ This-> GoodPrice = $ sGoodPrice;
$ This-> MaxCount = $ sMaxCount;

// Calculate the total number of items
$ This-> SortCount = count ($ sId );

}

// ******** Private, calculate the value of each type of commodity and the total price of all commodities based on new data
Function Calculate ()
{
For ($ I = 0; $ I <$ this-> SortCount; $ I ++)
{
/* Calculate the value of each item. If the discount is 0, the discount price */
$ GiftPrice = ($ this-> Discount [$ I] = 0? $ This-> GoodPrice:
Ceil ($ this-> Price [$ I] * $ this-> Discount [$ I])/100 );
$ This-> TotalCost + = $ GiftPrice * $ this-> Count [$ I];
}
}

// The interface functions are as follows:

// *** Add one item
// Determine whether the product is blue or not. If yes, add count. Otherwise, add a new product.
// Change the session value first, and then call update () and calculate () to update the member variable.
Function Add ($ a_ID, $ a_Name, $ a_Price, $ a_Discount,
$ A_GoodPrice, $ a_MaxCount, $ a_Count)
{
Global $ sId, $ sName, $ sCount, $ sPrice, $ sDiscount,
$ SGoodPrice, $ sMaxCount;

$ K = count ($ sId );
For ($ I = 0; $ I <$ k; $ I ++)
{// First check whether such a product has been added
If ($ sId [$ I] ==$ a_ID)
{
$ SCount [$ I] + = $ a_Count;
Break;
}
}
If ($ I >=$ k)
{// If not, add a new product type.
$ SId [] = $ a_ID;
$ SName [] = $ a_Name;
$ SPrice [] = $ a_Price;
$ SCount [] = $ a_Count;
$ SGoodPrice [] = $ a_GoodPrice;
$ SDiscount [] = $ a_Discount;
$ SMaxCount [] = $ a_MaxCount;
}

$ This-> Update (); // updates the member data of the class.
$ This-> Calculate ();
}

// Remove a commodity
Function Remove ($ a_ID)
{
Global $ sId, $ sName, $ sCount, $ sPrice, $ sDiscount,
$ SGoodPrice, $ sMaxCount;

$ K = count ($ sId );
For ($ I = 0; $ I <$ k; $ I ++)
{
If ($ sId [$ I] ==$ a_ID)
{
$ SCount [$ I] = 0;
Break;
}
}

$ This-> Update ();
$ This-> Calculate ();
}

// Change the number of items
Function ModifyCount ($ a_ I, $ a_Count)
{
Global $ sCount;

$ SCount [$ a_ I] = $ a_Count;
$ This-> Update ();
$ This-> Calculate ();
}

/***************************
Clear all items
*****************************/
Function RemoveAll ()
{
Session_unregister ('sid ');
Session_unregister ('sname ');
Session_unregister ('sprice ');
Session_unregister ('sdiscount ');
Session_unregister ('sgoodprice ');
Session_unregister ('scount ');
Session_unregister ('smaxcount ');
$ This-> SortCount = 0;
$ This-> TotalCost = 0;
}

// Whether a product is already in blue. The parameter specifies the ID of the product.
Function Exists ($ a_ID)
{
For ($ I = 0; $ I <$ this-> SortCount; $ I ++)
{
If ($ this-> Id [$ I] = $ a_ID) return TRUE;
}
Return FALSE;
}

// The position of a product in blue
Function IndexOf ($ a_ID)
{
For ($ I = 0; $ I <$ this-> SortCount; $ I ++)
{
If ($ this-> Id [$ I] ==$ id) return $ I;
}
Return 0;
}

// Obtain the information of a commodity, the main function
// Returns an associated array,
Function Item ($ I)
{
$ Result [id] = $ this-> Id [$ I];
$ Result [name] = $ this-> Name [$ I];
$ Result [price] = $ this-> Price [$ I];
$ Result [count] = $ this-> Count [$ I];
$ Result [discount] = $ this-> Discount [$ I];
$ Result [goodprice] = $ this-> GoodPrice [$ I];
$ Result [maxcount] = $ this-> MaxCount [I];
Return $ Result;
}

// Obtain the total number of product types
Function CartCount ()
{
Return $ this-> SortCount;
}

// Obtain the total commodity value
Function GetTotalCost ()
{
Return $ this-> TotalCost;
}
}
?>

I hope this article will help you with PHP programming.

Related Article

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.