PHP use session to achieve the simplest way to implement the shopping cart class

Source: Internet
Author: User
Tags strlen

Class Cart
{
/**
* Function Name: AddItem
* Function function: Add product
* Input parameters: $data-------------product Array
* Function return value: None
* Other Description: Because the data is recorded in the session, so do not return
*/
function AddItem ($data) {
Global $config _kezhi;
if (Is_array ($data) &&!empty ($data)) {
foreach ($data as $key => $val) {
Add quantity and price if goods exist
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, add lettering
$_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 function: To determine whether this product exists
* Input parameters: $id----------Product ID
* Function return value: BOOL
* Other Notes: 2004-8-19
*/
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 Function: Modify the number of goods
* Input parameters: $id--------------Product ID
* Number of $num-------------commodities
* Function return Value: Return value description
* Other Notes: Description
*/
function Moditem ($id, $num)
{
$arr = $this->getitems ($id);
Add quantity and price if goods exist
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 Function: Modify the product wrist circumference
* Input parameters: $id--------------Product ID
* $num-------------Commodity wrist circumference
* Function return Value: Return value description
* Other Notes: Description
*/
function Moditem_wanwei ($id, $wanwei)
{
$arr = $this->getitems ($id);
Add quantity and price if goods exist
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 goods exist
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, $kezhi 1= "", $kezhi 2 = "")
{
Global $config _kezhi;
$arr = $this->getitems ($id);
Add quantity and price if goods exist
Var_dump ($_session[' cart '));
Die ();
if ($this->_isexists ($id))
{
if (strlen ($_session[' cart '] [$id] ["Kezhi"]) = = 0 && strlen ($kezhi)!= 0) {//no lettering, add lettering
$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"] = $kezhi 1;
$_session[' cart ' [$id] ["kezhi2"] = $kezhi 2;
$_session[' cart ' [$id] ["count"] = round ($arr [' Price ']*intval ($num), 2);
}elseif (strlen ($_session[' cart '] [$id] ["Kezhi"])!= 0 && strlen ($kezhi) = = 0) {//have lettering, remove lettering
$_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"] = $kezhi 1;
$_session[' cart ' [$id] ["kezhi2"] = $kezhi 2;
}elseif (strlen ($_session[' cart '] [$id] ["Kezhi"])!= 0 && strlen ($kezhi)!= 0) {//have lettering, change 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"] = $kezhi 1;
$_session[' cart ' [$id] ["kezhi2"] = $kezhi 2;
}
}
}

/**
* Function Name: GetItems
* Function function: Get the product array
* Input Parameters: $id---------------The ID of a product
* Function return Value: Array
* Other Notes: 2004-8-19
*/
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 product
    *   input parameters:   $id-----------Commodity ID
& nbsp;   *   function return value: BOOL
    *   Other description:   2004-8-19
    */
    function Emptyitem ($id =null)
    {
         if ($id ==null)
        {
             unset ($_session[' cart '));
       }
        Else
        {
             unset ($_session[' cart '] [$id]);
       }
   }

/**
* Function Name: sum
* Function Function: Statistic Total Price
* Input Parameters: None
* Function return value: int
* Other Notes: 2004-8-19
*/
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.