[php]簡單的購物車範例

簡單的購物車範例,不需要操作Session,範例有新增、修改與刪除購物車等,主要是修改 [Class]參考 sCart 建立的購物車 Class,這個範例年代久遠,所以在使用時有發生問題也不知道怎麼處理,所以小修了一下。

程式的概念是有一個產品展示頁面,有加入購物車功能,加入購物車後,可以至下一頁修改數量或刪除產品,修改或刪除產品時總金額也會隨著一起更新,再下一頁就可以看到最後結帳的最終資訊,理論上可以在這裡製作聯絡資料的表單。

產品展示頁面

product.png

原始碼Product.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>購物車加入範例</title>
<script type="text/javascript" src="js/menu.js"></script>
<link rel="shortcut icon" href="images/i.ico" />
</head>
<body>
<div class="right_main">

<div class="product clearfix">
<div class="product_body fle">
<div class="product_poto fle"></div>
<div class="product_word fle">
<div class="product_word_tit">測試資料</div>
<div class="product_word_word">規 格:122<br />
<span class="product_word_word_w1">定 價:0 元</span><br />

<span class="product_word_word_w2">售 價:200 元<br />
</span></div>
<div class="product_word_shopping"><input class="product_bt"
    style="cursor: pointer;" onclick="location='addItem.php?sn=1';"
    value="加入購物車" type="button" /></div>
</div>
</div>

<div class="product_body fle">

<div class="product_poto fle"></div>
<div class="product_word fle">
<div class="product_word_tit">test</div>
<div class="product_word_word">規 格:122<br />
<span class="product_word_word_w1">定 價:1 元</span><br />
<span class="product_word_word_w2">售 價:1 元<br />

紅利金:10 元 </span></div>
<div class="product_word_shopping"><input class="product_bt"
    style="cursor: pointer;" onclick="location='addItem.php?sn=3';"
    value="加入購物車" type="button" /></div>
</div>
</div>

<div class="product_body fle">
<div class="product_poto fle"></div>

<div class="product_word fle">
<div class="product_word_tit">測試資料2</div>
<div class="product_word_word"><br />
<span class="product_word_word_w1">定 價:0 元</span><br />
<span class="product_word_word_w2">售 價:0 元<br />
</span></div>
<div class="product_word_shopping"><input class="product_bt"
    style="cursor: pointer;" onclick="location='addItem.php?sn=2';"
    value="加入購物車" type="button" /></div>
</div>
</div>

</div>
</div>
</body>
</html>

加入購物車addItem.php

<?php
header('Content-type: text/html; charset=utf-8');

require_once "inc/class/Car.class.php";

session_start();

$Cart = new Cart();

$sn = $_GET["sn"];
$num = $_GET['num'];
//echo $sn;
if(!is_numeric($num))
$num = 1;
if(isset($sn) && strlen(trim($sn))>0 && is_numeric($sn)){
    $Cart->addItem($sn, "產品名稱", 100, $num, "描述", "單位");

}
//加入成功後回到前一頁
$referer  = $_SERVER['HTTP_REFERER'];
if(strlen(trim($referer))==0)
$referer = "Product.php";

header("Location:$referer");
?>

購物車內容

MyCar.png

原始碼MyCar.php

<?php
header('Content-type: text/html; charset=utf-8');

include "inc/class/Car.class.php";
session_start();

$MyCart = new Cart();

$Myitems = $MyCart->getAllItems();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>我的購物車</title>

<link href="css/styleMask.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/MyCar.js"></script>
</head>

<body>

<div class="right_main">
<div class="product">
<div class="product_b_body">
<form id="myform" action="OrderForm.php" method="post"
    onsubmit="return checkForm();">
<div class="shopping">
<div class="shopping_body">
<table width="100%" border="0" cellpadding="6" cellspacing="0"
    id="mytable">
    <tr>
        <td width="43%" class="shopping_w1"
            style="border-right: 1px solid #d2d2d2; border-bottom: 1px solid #d2d2d2;">商品名稱</td>
        <td width="21%" class="shopping_w1"
            style="border-right: 1px solid #d2d2d2; border-bottom: 1px solid #d2d2d2;">價
        格</td>
        <td width="21%" class="shopping_w1"
            style="border-right: 1px solid #d2d2d2; border-bottom: 1px solid #d2d2d2;">數
        量</td>
        <td width="15%" style="border-bottom: 1px solid #d2d2d2;">
        <table width="96" border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td width="70%" align="center" valign="middle"></td>
                <td width="30%" align="center" valign="middle"></td>
            </tr>
        </table>
        </td>
    </tr>
    <?php

    $checkcount = 0;
    if ($Myitems)
    {
        foreach ($Myitems as $key => $Myitem)
        {
            $checkcount ++;
            $background  = $checkcount%2==1?"bgcolor=\"#e7e7e7\"":"";
            //var_export($Myitem);
            ?>
    <tr id="item_<?php echo $Myitem->_sn;?>">
        <td <?php echo $background;?>><input type="hidden"
            name="item<?php echo $Myitem->_sn;?>"
            value="<?php echo $Myitem->_sn;?>"></input> <?php echo $Myitem->_name; ?></td>
        <td <?php echo $background;?>><?php echo $Myitem->_price; ?>元</td>
        <td <?php echo $background;?>><select
            name="Quity<?php echo $Myitem->_sn;?>" class="shopping_down"
            onchange="amount();">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
        </select></td>
        <td <?php echo $background;?>>
        <table width="96" border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td width="70%" align="center" valign="middle"><input
                    name="button<?php echo $Myitem->_sn; ?>" type="button"
                    class="shopping_bt" style="cursor: pointer;"
                    onclick="del(<?php echo $Myitem->_sn; ?>);" value="刪 除" /></td>
                <td width="30%" align="center" valign="middle"></td>
            </tr>
        </table>
        </td>
    </tr>
    <?php
        }
    }
    else{
        ?>
    <tr>
        <td bgcolor="#e7e7e7" colspan="4">目前無任何購物資料</td>
    </tr>
    <?php
    }
    ?>
</table>

</div>
</div>
<table  border="0" cellpadding="5" cellspacing="0"
    style="margin-top: 10px;">
    <tr>
        <td colspan="2" align="right">總金額:<span class="shopping_w2"
            id="amount">0</span>元</td>
    </tr>

</table>
<script type="text/javascript">amount();</script>

<table  border="0" cellpadding="0" cellspacing="0"
    style="margin-top: 10px;">
    <tr>
        <td align="right"><input name="Submit2" type="submit"
            class="shopping_bt1" style="cursor: pointer;" value="下一步" /></td>
    </tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>

修改數量-總金額會更動(javascript)

MyCar2.png

刪除時總金額也會更動-ajax修改購物車

MyCar-delete.png

原始碼delItem.php

<?php
header('Content-type: text/html; charset=utf-8');
require_once "inc/class/Car.class.php";
//request.getHeader("referer");
session_start();

$Cart = new Cart();


$sn = $_GET["sn"];


if(isset($sn) && strlen(trim($sn))>0 && is_numeric($sn)){

    $Cart->removeItem($sn);
    //echo '刪除成功!';
}

?>

修改及刪除MyCar.js

function del(id) {
    this.id = id;
    new Ajax.Request('DelItem.php?sn=' + id, {
        method : 'GET',
        onCreate : function() {

        },
        onFailure : function() {

        },
        onSuccess : function(t) {
            if (t.responseText.blank()) {
                $("item_" + id).remove();

            } else {
                alert(t.responseText);
            }
        }.bind(this),
        onComplete : function() {

            amount();
        }
    });

    return false;
}
function amount() {

    var sum = 0;
    try {
        var first = $("mytable").down("tr", 2);
        if ($("mytable").down("tr", 2).down("select")) {
            
            do {
                sum += first.down("select").getValue()
                        * parseInt(first.down("td", 1).innerHTML);

            } while ((first = first.next()))
        }
    } catch (e) {
    }
    $("amount").update(sum);
}
function checkForm() {

    return true;
}

確認表單

在上一頁確認後,真實修改至購物車產品的數量

OrderForm.png

原始碼OrderForm.php

<?php
header('Content-type: text/html; charset=utf-8');

include "inc/class/Car.class.php";
session_start();

$MyCart = new Cart();

$Myitems = $MyCart->getAllItems();
//var_export($Myitems);
foreach ($Myitems as $key => $Myitem)
{
    //$item = $Myitem->getItem();
    $q = $_POST['Quity'.$key];
    //echo $q;
    $Myitem->updateItem($q);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>我的購物車</title>

<link href="css/styleMask.css" rel="stylesheet" type="text/css" />

</head>

<body>

<div class="right_main">
<div class="product">
<div class="product_b_body">
<form id="myform" action="OrderForm.php" method="post"
    onsubmit="return checkForm();">
<div class="shopping">
<div class="shopping_body">
<table width="100%" border="0" cellpadding="6" cellspacing="0"
    id="mytable">
    <tr>
        <td width="43%" class="shopping_w1"
            style="border-right: 1px solid #d2d2d2; border-bottom: 1px solid #d2d2d2;">商品名稱</td>
        <td width="21%" class="shopping_w1"
            style="border-right: 1px solid #d2d2d2; border-bottom: 1px solid #d2d2d2;">價
        格</td>
        <td width="21%" class="shopping_w1"
            style="border-right: 1px solid #d2d2d2; border-bottom: 1px solid #d2d2d2;">數
        量</td>

    </tr>
    <?php

    $checkcount = 0;
    if ($Myitems)
    {
        foreach ($Myitems as $key => $Myitem)
        {
            $checkcount ++;
            $background  = $checkcount%2==1?"bgcolor=\"#e7e7e7\"":"";
            //var_export($Myitem);
            ?>
    <tr id="item_<?php echo $Myitem->_sn;?>">
        <td <?php echo $background;?>><input type="hidden"
            name="item<?php echo $Myitem->_sn;?>"
            value="<?php echo $Myitem->_sn;?>"></input> <?php echo $Myitem->_name; ?></td>
        <td <?php echo $background;?>><?php echo $Myitem->_price; ?>元</td>
        <td <?php echo $background;?>><?php echo $Myitem->_quantity;?></td>

    </tr>
    <?php
        }
    }
    else{
        ?>
    <tr>
        <td bgcolor="#e7e7e7" colspan="4">目前無任何購物資料</td>
    </tr>
    <?php
    }
    ?>
</table>

</div>
</div>

</form>
</div>
</div>
</div>
</body>
</html>

修改購物車UpdateItem.php

<?php
header('Content-type: text/html; charset=utf-8');
require_once "inc/class/Car.class.php";

session_start();

$Cart = new Cart();


$sn = $_GET["sn"];
$num = $_GET['num'];

if(!is_numeric($num))
$num = 1;
if(isset($sn) && strlen(trim($sn))>0 && is_numeric($sn)){

    $Cart->updateItem($sn,$num);
    //修改$sn的數量
}

?>

購物車Cart.class.php

<?php
// 商品項目類別
class CartItem
{
    var $_sn;
    var $_name;
    var $_price;
    var $_quantity;
    var $_brief;
    var $_spec;
    var $_sub_total;

    function CartItem($_sn, $_name, $_price, $_quantity, $_brief, $_spec)
    {
        $this->_init();

        $this->_sn = $_sn;
        $this->_name = $_name;
        $this->_price = $_price;
        $this->_quantity = $_quantity;
        $this->_brief = $_brief;
        $this->_spec = $_spec;
         
        $this->_calc();
    }

    // 更新商品項目的數量
    function updateItem($quantity)
    {
        $this->_quantity = $quantity;
        $this->_calc();
    }

    // 取得該項商品的價格小計
    function getSubTotal()
    {
        return $this->_sub_total;
    }

    // 取得該項商品的資訊
    function getItem()
    {
        $item = array();

        $item['sn'] = $this->_sn;
        $item['name'] = $this->_name;
        $item['price'] = $this->_price;
        $item['quantity'] = $this->_quantity;
        $item['brief'] = $this->_brief;
        $item['spec'] = $this->_spec;
        $item['sub_total'] = $this->_sub_total;

        return $item;
    }

    function _init()
    {
        $_sn = "";
        $_name = "";
        $_price = 0;
        $_quantity = 0;
        $_brief = "";
        $_spec = "";
        $_sub_total = 0;
    }

    // 計算該項商品的小計價格
    function _calc()
    {
        $this->_sub_total = $this->_quantity * $this->_price;
    }

}

// 購物車主類別
class Cart
{
    var $_items = array();
    var $_total = 0;

    function Cart()
    {
        $this->_init();
    }

    // 新增一項商品
    function addItem($sn, $name, $price, $quantity, $brief, $spec)
    {
        if (!is_object($this->_items[$sn]))
        {
            $this->_items[$sn] = new CartItem($sn, $name, $price, $quantity, $brief, $spec);

            $this->_refresh();
            $_SESSION['Cart'] = $this;
        }
    }

    // 更新某項商品的數量
    function updateItem($sn, $quantity)
    {
        if (is_object($this->_items[$sn]))
        {
            $this->_items[$sn]->updateItem($quantity);
            //$_SESSION['Cart']['items'][$sn] = $this->_items[$sn]->getItem();
            $this->_refresh();
            $_SESSION['Cart'] = $this;
        }
    }

    // 移除某項商品
    function removeItem($sn)
    {
        if (is_object($this->_items[$sn]))
        {
            unset ($this->_items[$sn]);
            //unset ($_SESSION['Cart']['items'][$sn]);
            $this->_refresh();
            $_SESSION['Cart'] = $this;
        }
    }

    // 清空購物車
    function clearCart()
    {
        unset ($this->_items);
        unset ($_SESSION['Cart']);
        $this->_init();
    }

    // 取得總價
    function getTotal()
    {
        $this->_refresh();
        return $this->_total;
    }

    // 取得所有的商品項目
    function getAllItems()
    {

        if(count($this->_items))
        {

            return $this->_items;

        }
        else
        {
            return false;
        }
    }

    // 初始化
    function _init()
    {
        // 如果購物車資料已經存在了

         if(!isset($_SESSION['Cart']))
         {
            $this->_items = array();
            $this->_total = 0;
            $_SESSION['Cart']= $this;
            //echo 123;
         }
           else
           {
               
               $cart = $_SESSION['Cart'];

               $this->_items = $cart->_items;
               $this->_total = $cart->_total;
               
               $this->_refresh();
               
           }
        
    }

    // 重新計算總價
    function _refresh()
    {
        $this->_total = 0;

        reset ($this->_items);
        foreach ($this->_items as $key => $item)
        {
            $this->_total += $item->getSubTotal();
        }
        reset ($this->_items);

    }
}

?>
skydrive 站內
原始碼

80 thoughts to “[php]簡單的購物車範例”

  1. 請問一下喔!!

    在商品頁面上的庫存量

    假設 a物品 庫存:10

    有個客戶在購物車上放了a物品2個

    同時間在商品頁面上的庫存量自動修改為:8

    避免同時間多人訂購 數量超出庫存

    1. 只要在最後訂單確認的那個點確認是否有庫存
      有的話再去update -2個就好了…
      比較單純

  2. 請問~
    如果購物車要加上
    任選兩件299的話
    應該在哪裡加入判斷
    謝謝

    1. 如果要很簡單的判斷,只需要在購物車列表那裡去判定就可以了….不過應該特別的狀況會很多

  3. 您好~我下載整個檔案測試
    按加入購物車都沒反應
    我發現少了menu.js檔案
    是不是與這個有關係??
    謝謝

  4. 請問一下@@
    為什麼在確認表單時的商品數量沒有更新到?

    1. 你確認一下在session start前是不是有錯誤訊息
      session可能沒存到,還有要注意程式檔案編碼要一樣

  5. 不好意思我想請問一下
    我發現加入購物車執行不了
    看一下程式碼
    卻少這兩個檔案
    menu.js prototype.js

    這會影響到程式碼嗎?

    1. 會 javascript在執行時 最少要prototype.js
      您可以直接去prototype.js官網下載

  6. 請問一下購物車內容中的產品名稱要如何直接抓取產品展示頁面中的產品名稱 例如你php黨中的測試資料

  7. 不好意思, 請問一下, 為什麼我點了加入購物車, 出現的是下載addItem.php ?

    1. 1.請先確認您的環境是否能執行php
      2.header部份請再檢查一次
      header(‘Content-type: text/html; charset=utf-8’);
      要為text/html
      3.再不行請檢查您apache的設定(有問題機會不大)

  8. 想請問
    我點了兩個商品加入購物車
    如何在Product.php頁面看到目前有”(2)商品在購物車”?

    1. @漢堡, $MyCart->getAllItems();看他的數量有多少 就是有多少商品在購物車了

  9. 有個最大的問題>”<
    不要用下拉選單改成text
    這部份該怎麼調整?

      1. @yku, 有個問題可能要麻煩您幫我看一下code哪邊出問題
        請問方便給我信箱嗎?

  10. 請問原始碼壓縮檔在何處可下載?
    >[php]簡單的購物車範例

  11. 請問如何增加產品?
    因為sn1 2 3 都是一樣的商品耶
    謝謝~(我是新手)

  12. 想請問我的程式碼這哪裡有問題嗎

    我按下加入購物車後 進入頁面時
    出現以下程式碼 ,這問題出在哪呢?
    還有其他php 執行都無法顯示頁面,也都是出現程式碼,目前只有product.php 可以看到
    這問題在哪呢? 感謝大大的提供
    0 && is_numeric($sn)){
    $Cart->addItem($sn, “�W��迂”, 100, $num, “�膩”, “�桐�”);

    }
    //���敺��啣�銝€��
    $referer = $_SERVER[‘HTTP_REFERER’];
    if(strlen(trim($referer))==0)
    $referer = “Product.php”;

    header(“Location:$referer”);
    ?>

      1. @yku, 我是用dreamweaver 寫php 不知為什麼執行後 顯示的是程式碼

        還有購物車 按下去後 也不會跳頁面 拜託幫幫我

        你有更清楚的購物車 程式嗎??? 求求幫助我~~~ 我卡了好多天~~~ 拜託你

        1. 我的購物車程式碼不是就附在上面了?你想怎樣的更清楚
          把dreamviewer的文件編碼改成utf8就好了
          你在linux base的主機到時一定要改的
          windows的有時好像混的過去
          如果不會改
          把程式碼用記事本打開,再copy貼到你用的dreamviewer上去
          就看的到了~~會不會有其它執行問題就不知道了
          重點在於你改用utf8的文件格式

  13. yku大,你好!
    加入商品後我在MyCar.php 查看是得出以下結果:

    商品名稱 價 格 數 量
    產品名稱 100元
    產品名稱 100元
    產品名稱 100元

    總金額:300元
    —————————————-
    而並不是Product.php 裡面的商品名稱及價格,請問我是那裡出錯了?煩請協助一下,謝謝~~

  14. 你好阿~我與樓上的問題是一樣,要怎樣更改才能顯示我的PRODUCT.PHP裡的產品名稱?

    additem.php裡的
    if(isset($sn) && strlen(trim($sn))>0 && is_numeric($sn)){
    $Cart->addItem($sn, “產品名稱”, 100, $num, “描述”, “單位”);

    我的產品名稱及價格 都不是我的PRODUCT.PHP裡的資料是要在這行修改嗎? 要怎樣修改???

    1. @YUKIII,
      1.你把”產品名稱”那裡加一個sql
      去query 傳來sn的產品名稱 描述跟單位 然後取代
      $Cart->addItem($sn, “產品名稱”, 100, $num, “描述”, “單位”);
      就好了
      2.把產品名稱等利用post(或get)傳進來使用

      1. 你好,我也遇到同樣的問題,但是看不太懂你打的那幾點,有辦法再打清楚一點嗎~~

  15. 大大你好
    不好意思請問一下
    我將你的範例下載下來測試
    用Dreamweaver,CS6直接本機測試
    按下”加入購物車”後 就變成這樣
    http://i.imgur.com/68BCKFK.jpg
    請問大大是甚麼問題呢?

  16. 大大您好
    不好意思我是初次設計網頁以及接觸PHP
    我想學習你的購物車範例下去更改我的網站

    可是我的網站目前規劃是
    每個商品的頁面是獨立的(每個商品有自己的介紹頁面)
    而且商品顯示的只有商品名稱,價格,訂購數量
    可以請問大大如何更改嗎?

    1. @Miak,
      你更改Product.php變成你要的格式
      介紹頁面的話,在Product.php加個明細按鈕click後
      進入ProductDetail.php?id=產品id

  17. yku您好
    我已經成功了
    感謝你的指導
    先跟你說聲謝謝

    可是自己發現另外一個問題是
    我用不同帳號去登入,結果購物車的內容是一樣的
    也就是不會依據帳號不同而購物車會有不同 ..
    請問這有辦法更改嗎 .. ?

    1. 因為基本上購物車,是使用同一個SESSION key去存放的
      $_SESSION[‘Cart’]
      你可以把Cart改成user的id
      這樣子不同使用者就有不同的購物車了
      不過相對的變成要強迫使用者先登入才能購物

  18. 謝謝指導
    可是依據更改session後的結果

    就會出現下面的狀況

    Warning: reset() [function.reset]: Passed variable is not an array or object in C:\AppServ\www\a\car\inc\class\Car.class.php on line 184

    Warning: Invalid argument supplied for foreach() in C:\AppServ\www\a\car\inc\class\Car.class.php on line 185

    Warning: reset() [function.reset]: Passed variable is not an array or object in C:\AppServ\www\a\car\inc\class\Car.class.php on line 189

    ——-程式碼有出現錯誤的是以下的區塊
    // 重新計算總價
    function _refresh()
    {
    $this->_total = 0;

    reset ($this->_items);
    foreach ($this->_items as $key => $item)
    {
    $this->_total += $item->getSubTotal();
    }
    reset ($this->_items);

    }

    這方面一直無法解決 ..
    懇請大大再指導一次

    1. @Miak,
      可以給我你改的部份嘛?
      這看起來應該是取得session裡面的值有錯誤

  19. 大大你好
    我是剛接觸PHP的初學者
    目前要寫一個有購物車功能的平台
    套用了你的這個購物車功能
    現在目前遇到的問題是
    我想最後送出能把session裡面所記錄的產品名稱及數量存入MYSQL的資料表裡
    想問如何實現

    1. @Alan,
      你看
      OrderForm.php
      它可以取出Session的內容,
      你只要把取出的內容存入mysql就好了

  20. 大大你好
    請問如何給你那些程式碼?
    我的網頁文件都要給你嗎??

    然後現在是卡在要將購物車的商品資料存入資料庫
    但是還有收件者的資料

    問題在於 大大的購物車是用foreach,如果取 $Myitem->_name 則只會取到最後一個
    這方面找不到一個好方法解決

    還有就是如果要存入資料庫的話
    是要將商品資料與收件者資料放在同一頁面嗎 ?
    因為設定是當填寫完收件者資料才會存進去
    如果是不同頁面的話就需要將購物車的資料先傳到填寫資料的那一頁 ..

    目前的瓶頸 ..

  21. 已經有寄信到信箱了QQ
    希望大大幫我解決數量無法儲存的問題
    知道問題點在於
    那一頁面submit是用 action=”?job=submit” 的方法
    所以按下去之後
    頁面會刷新一次
    商品的名稱與價格是存在商品資料庫 所以不會跑掉
    可是數量是上一頁的值
    刷新等於是沒再傳送 所以會跑掉 …
    導致存的時候變成0

  22. 版主你好~
    我用了此購物車~修改之後~很正常加入購物車~也可以顯示出資料~
    就是刪除單筆資料無法執行~不知道是哪裡錯誤~
    我全部的地方都看過了~就是找不到問題點~
    還請大大幫忙~~

  23. 大大您好!
    感謝您的購物車程式,非常受用!
    想請教如果要重複購買同一件商品,該如何解決?

    1. 會建議他更改數量就好了
      這樣子商品要出貨時比較好知道正確的數量
      對撿貨這件事來說這樣子似乎比較好

  24. 大大您好! 感謝您的回覆!
    再想請問,目前MyCart.php表格下方的總金額都一直是0元,如何才會自動加總呢?
    另外想請教,如果不同電腦進入線上的購物車系統,會看到其他人的購物內容嗎?

    1. @Adam,
      您檢查看看有沒有javascript錯誤
      MyCar.js
      function amount() {

      var sum = 0;
      try {
      var first = $(“mytable”).down(“tr”, 2);
      if ($(“mytable”).down(“tr”, 2).down(“select”)) {

      do {
      sum += first.down(“select”).getValue()
      * parseInt(first.down(“td”, 1).innerHTML);

      } while ((first = first.next()))
      }
      } catch (e) {
      }
      $(“amount”).update(sum);
      }

  25. 大大您好!
    Could you teach me the code how to get the “總金額” at “mycar.php” so that I could submit to payment gateway? Thanks very much.

    <input type="hidden" name="amount" value="”> // <- ***HERE

  26. @yku

    您好
    想請問您一下
    再將購物車裡的東西算出總金額後
    我想按下一頁進入結帳系統
    也就是同時顯示選購商品及金額
    並要求輸入運送地址 姓名 及電話
    請問這樣是不是還要再額外新增一個結帳資料庫
    另外應該如何達成這個步驟呢?
    (購物車的部分已做完 差最後這一步…)

    謝謝您了!!!

    1. @yayaya,

      你可以建一個table來存您的訂單
      應該說二個
      一個存主檔(訂購人資訊),一個存購物明細

  27. 版大不好意思
    我找不到你程式下載的地方
    是否可以mial給我呢
    感謝

      1. @yku,
        大大您好,可以請問一下,我執行程式後畫面顯示

        商品名稱 價 格 數 量
        產品名稱 100元
        產品名稱 100元
        產品名稱 100元

        總金額:300元
        —————————————-
        並不是Product.php 所設定的商品名稱,而是 addItem.php 所設定的資料
        大大有說前面加 sql 取回資料,請問要 sql 需要怎麼加?
        試過 $sql = $Cart->addItem($sn, “產品名稱”, 100, $num, “描述”, “單位”);
        還是不行
        小弟我是新手,可以請大大指教嗎

  28. 版大您好,載點失連了,可以麻煩再補上檔案嗎?謝謝您~

  29. 版大您好

    如果我同一個商品有兩個規格,但SN都是同一個,購買狀況可能會有兩種規格加入購物車,請問這樣做怎樣會比較好?

  30. 版大您好
    if(isset($sn) && strlen(trim($sn))>0 && is_numeric($sn)){
    $Cart->addItem($sn, “產品名稱”, 100, $num, “描述”, “單位”)

    我想問一下num是甚麼意思~?

    該如何把網頁的商品名稱跟價錢帶到購物車裡?

    小弟是新手QQ

        1. 程式碼上有喔
          Class Car裡
          // 清空購物車
          function clearCart()
          {
          unset ($this->_items);
          unset ($_SESSION[‘Cart’]);
          $this->_init();
          }

  31. 請問我按下加入購物車後 為什麼顯示的連結頁面只是程式碼 ?

    1. 顯示的如下
      0 && is_numeric($sn)){
      $Cart->addItem($sn, “�?����滨迂”, 100, $num, “��讛膩”, “�鱓雿�”);

      }
      //��惩�交�?�笔���?���滢���
      $referer = $_SERVER[‘HTTP_REFERER’];
      if(strlen(trim($referer))==0)
      $referer = “Product.php”;

      header(“Location:$referer”);
      ?>

  32. 感謝大大分享購物車程式 不過我再將它精簡遇到個問題
    prototype.js 檔案 是放在官網公開的嗎? 他的用意在哪呢?
    謝謝

  33. 您好
    謝謝作者放出文章,感覺受益良多
    有個問題想要請教一下
    清空購物車方面 我看到有function clearCart()
    但是該如何去呼叫使用呢 直接echo 好像沒有作用
    小弟後來直接用session_destroy 來清除
    但是有點不甘心阿~~

Adam 發表迴響取消回覆