God is not home today

首頁 | 網路廣播電台 | 網誌聯播 | 商品推薦

Ecshop 限制商品最大訂購數量

很多站長都希望可以限制最大訂購數量,避免在商品價格輸入錯誤的狀況下產生太大的損失,以下的修改可以透過後台設定商品的最大訂購數量,修改完成後,所有已上架商品的最大訂購數量預設值為0,而當最大訂購數量為0時,則表示此商品不限制最大訂購數量

步驟一:
商品資料表必須新增一個最大訂購數量限制的欄位->max_number
打開goods資料表,在warn_number欄位後面新增一個max_number,tinyint類型預設值為0,代表不限制最大訂購數量,如果不會新增資料表欄位,請執行下面SQL語法:

SQL語法(請將`[前綴字串_]goods`修改為您資料表的前綴字串,例如您的資料表前綴為ecs_,請修改為`ecs_goods`):

alter table `[前綴字串_]goods` add column `max_number` tinyint (3) UNSIGNED  DEFAULT '0' NOT NULL  after `warn_number`

步驟二:
在後台新增商品時必須有個欄位設定最大訂購數量:

開啟admin/goods.php,大約在174行與242行的位置找到

'warn_number'   => 1,

在其後面插入一行:

'max_number'    => 0,

大約在808行的位置找到

$warn_number = isset($_POST['warn_number']) ? $_POST['warn_number'] : 0;

在後面新增一行

$max_number = isset($_POST['max_number']) ? $_POST['max_number'] : 0;

接著修改新增商品的SQL語法,大約在823行的位置開始,找到下列整串語法:

if ($is_insert)
    {
        if ($code == '')
        {
            $sql = "INSERT INTO " . $ecs->table('goods') . " (goods_name, goods_name_style, goods_sn, " .
                    "cat_id, brand_id, shop_price, market_price, is_promote, promote_price, " .
                    "promote_start_date, promote_end_date, goods_img, goods_thumb, original_img, keywords, goods_brief, " .
                    "seller_note, goods_weight, goods_number, warn_number, integral, give_integral, is_best, is_new, is_hot, " .
                    "is_on_sale, is_alone_sale, is_shipping, goods_desc, add_time, last_update, goods_type, rank_integral, suppliers_id)" .
                "VALUES ('$_POST[goods_name]', '$goods_name_style', '$goods_sn', '$catgory_id', " .
                    "'$brand_id', '$shop_price', '$market_price', '$is_promote','$promote_price', ".
                    "'$promote_start_date', '$promote_end_date', '$goods_img', '$goods_thumb', '$original_img', ".
                    "'$_POST[keywords]', '$_POST[goods_brief]', '$_POST[seller_note]', '$goods_weight', '$goods_number',".
                    " '$warn_number', '$_POST[integral]', '$give_integral', '$is_best', '$is_new', '$is_hot', '$is_on_sale', '$is_alone_sale', $is_shipping, ".
                    " '$_POST[goods_desc]', '" . gmtime() . "', '". gmtime() ."', '$goods_type', '$rank_integral', '$suppliers_id')";
        }
        else
        {
            $sql = "INSERT INTO " . $ecs->table('goods') . " (goods_name, goods_name_style, goods_sn, " .
                    "cat_id, brand_id, shop_price, market_price, is_promote, promote_price, " .
                    "promote_start_date, promote_end_date, goods_img, goods_thumb, original_img, keywords, goods_brief, " .
                    "seller_note, goods_weight, goods_number, warn_number, integral, give_integral, is_best, is_new, is_hot, is_real, " .
                    "is_on_sale, is_alone_sale, is_shipping, goods_desc, add_time, last_update, goods_type, extension_code, rank_integral)" .
                "VALUES ('$_POST[goods_name]', '$goods_name_style', '$goods_sn', '$catgory_id', " .
                    "'$brand_id', '$shop_price', '$market_price', '$is_promote','$promote_price', ".
                    "'$promote_start_date', '$promote_end_date', '$goods_img', '$goods_thumb', '$original_img', ".
                    "'$_POST[keywords]', '$_POST[goods_brief]', '$_POST[seller_note]', '$goods_weight', '$goods_number',".
                    " '$warn_number', '$_POST[integral]', '$give_integral', '$is_best', '$is_new', '$is_hot', 0, '$is_on_sale', '$is_alone_sale', $is_shipping, ".
                    " '$_POST[goods_desc]', '" . gmtime() . "', '". gmtime() ."', '$goods_type', '$code', '$rank_integral')";
        }
    }
    else
    {
        //如果有上传图片,删除原来的商品图
        $sql = "SELECT goods_thumb, goods_img, original_img " .
                    " FROM " . $ecs->table('goods') .
                    " WHERE goods_id = '$_REQUEST[goods_id]'";
        $row = $db->getRow($sql);
        if ($proc_thumb && $goods_img && $row['goods_img'] && !goods_parse_url($row['goods_img']))
        {
            @unlink(ROOT_PATH . $row['goods_img']);
            @unlink(ROOT_PATH . $row['original_img']);
        }

        if ($proc_thumb && $goods_thumb && $row['goods_thumb'] && !goods_parse_url($row['goods_thumb']))
        {
            @unlink(ROOT_PATH . $row['goods_thumb']);
        }

        $sql = "UPDATE " . $ecs->table('goods') . " SET " .
                "goods_name = '$_POST[goods_name]', " .
                "goods_name_style = '$goods_name_style', " .
                "goods_sn = '$goods_sn', " .
                "cat_id = '$catgory_id', " .
                "brand_id = '$brand_id', " .
                "shop_price = '$shop_price', " .
                "market_price = '$market_price', " .
                "is_promote = '$is_promote', " .
                "promote_price = '$promote_price', " .
                "promote_start_date = '$promote_start_date', " .
                "suppliers_id = '$suppliers_id', " .
                "promote_end_date = '$promote_end_date', ";

        //如果有上传图片,需要更新数据库
        if ($goods_img)
        {
            $sql .= "goods_img = '$goods_img', original_img = '$original_img', ";
        }
        if ($goods_thumb)
        {
            $sql .= "goods_thumb = '$goods_thumb', ";
        }
        if ($code != '')
        {
            $sql .= "is_real=0, extension_code='$code', ";
        }
        $sql .= "keywords = '$_POST[keywords]', " .
                "goods_brief = '$_POST[goods_brief]', " .
                "seller_note = '$_POST[seller_note]', " .
                "goods_weight = '$goods_weight'," .
                "goods_number = '$goods_number', " .
                "warn_number = '$warn_number', " .
                "integral = '$_POST[integral]', " .
                "give_integral = '$give_integral', " .
                "rank_integral = '$rank_integral', " .
                "is_best = '$is_best', " .
                "is_new = '$is_new', " .
                "is_hot = '$is_hot', " .
                "is_on_sale = '$is_on_sale', " .
                "is_alone_sale = '$is_alone_sale', " .
                "is_shipping = '$is_shipping', " .
                "goods_desc = '$_POST[goods_desc]', " .
                "last_update = '". gmtime() ."', ".
                "goods_type = '$goods_type' " .
                "WHERE goods_id = '$_REQUEST[goods_id]' LIMIT 1";
    }

將這整串語法以下列語法取代:

/*
    if ($is_insert)
    {
        if ($code == '')
        {
            $sql = "INSERT INTO " . $ecs->table('goods') . " (goods_name, goods_name_style, goods_sn, " .
                    "cat_id, brand_id, shop_price, market_price, is_promote, promote_price, " .
                    "promote_start_date, promote_end_date, goods_img, goods_thumb, original_img, keywords, goods_brief, " .
                    "seller_note, goods_weight, goods_number, warn_number, integral, give_integral, is_best, is_new, is_hot, " .
                    "is_on_sale, is_alone_sale, is_shipping, goods_desc, add_time, last_update, goods_type, rank_integral, suppliers_id)" .
                "VALUES ('$_POST[goods_name]', '$goods_name_style', '$goods_sn', '$catgory_id', " .
                    "'$brand_id', '$shop_price', '$market_price', '$is_promote','$promote_price', ".
                    "'$promote_start_date', '$promote_end_date', '$goods_img', '$goods_thumb', '$original_img', ".
                    "'$_POST[keywords]', '$_POST[goods_brief]', '$_POST[seller_note]', '$goods_weight', '$goods_number',".
                    " '$warn_number', '$_POST[integral]', '$give_integral', '$is_best', '$is_new', '$is_hot', '$is_on_sale', '$is_alone_sale', $is_shipping, ".
                    " '$_POST[goods_desc]', '" . gmtime() . "', '". gmtime() ."', '$goods_type', '$rank_integral', '$suppliers_id')";
        }
        else
        {
            $sql = "INSERT INTO " . $ecs->table('goods') . " (goods_name, goods_name_style, goods_sn, " .
                    "cat_id, brand_id, shop_price, market_price, is_promote, promote_price, " .
                    "promote_start_date, promote_end_date, goods_img, goods_thumb, original_img, keywords, goods_brief, " .
                    "seller_note, goods_weight, goods_number, warn_number, integral, give_integral, is_best, is_new, is_hot, is_real, " .
                    "is_on_sale, is_alone_sale, is_shipping, goods_desc, add_time, last_update, goods_type, extension_code, rank_integral)" .
                "VALUES ('$_POST[goods_name]', '$goods_name_style', '$goods_sn', '$catgory_id', " .
                    "'$brand_id', '$shop_price', '$market_price', '$is_promote','$promote_price', ".
                    "'$promote_start_date', '$promote_end_date', '$goods_img', '$goods_thumb', '$original_img', ".
                    "'$_POST[keywords]', '$_POST[goods_brief]', '$_POST[seller_note]', '$goods_weight', '$goods_number',".
                    " '$warn_number', '$_POST[integral]', '$give_integral', '$is_best', '$is_new', '$is_hot', 0, '$is_on_sale', '$is_alone_sale', $is_shipping, ".
                    " '$_POST[goods_desc]', '" . gmtime() . "', '". gmtime() ."', '$goods_type', '$code', '$rank_integral')";
        }
    }
    else
    {
        //如果有上传图片,删除原来的商品图
        $sql = "SELECT goods_thumb, goods_img, original_img " .
                    " FROM " . $ecs->table('goods') .
                    " WHERE goods_id = '$_REQUEST[goods_id]'";
        $row = $db->getRow($sql);
        if ($proc_thumb && $goods_img && $row['goods_img'] && !goods_parse_url($row['goods_img']))
        {
            @unlink(ROOT_PATH . $row['goods_img']);
            @unlink(ROOT_PATH . $row['original_img']);
        }

        if ($proc_thumb && $goods_thumb && $row['goods_thumb'] && !goods_parse_url($row['goods_thumb']))
        {
            @unlink(ROOT_PATH . $row['goods_thumb']);
        }

        $sql = "UPDATE " . $ecs->table('goods') . " SET " .
                "goods_name = '$_POST[goods_name]', " .
                "goods_name_style = '$goods_name_style', " .
                "goods_sn = '$goods_sn', " .
                "cat_id = '$catgory_id', " .
                "brand_id = '$brand_id', " .
                "shop_price = '$shop_price', " .
                "market_price = '$market_price', " .
                "is_promote = '$is_promote', " .
                "promote_price = '$promote_price', " .
                "promote_start_date = '$promote_start_date', " .
                "suppliers_id = '$suppliers_id', " .
                "promote_end_date = '$promote_end_date', ";

        //如果有上传图片,需要更新数据库
        if ($goods_img)
        {
            $sql .= "goods_img = '$goods_img', original_img = '$original_img', ";
        }
        if ($goods_thumb)
        {
            $sql .= "goods_thumb = '$goods_thumb', ";
        }
        if ($code != '')
        {
            $sql .= "is_real=0, extension_code='$code', ";
        }
        $sql .= "keywords = '$_POST[keywords]', " .
                "goods_brief = '$_POST[goods_brief]', " .
                "seller_note = '$_POST[seller_note]', " .
                "goods_weight = '$goods_weight'," .
                "goods_number = '$goods_number', " .
                "warn_number = '$warn_number', " .
                "integral = '$_POST[integral]', " .
                "give_integral = '$give_integral', " .
                "rank_integral = '$rank_integral', " .
                "is_best = '$is_best', " .
                "is_new = '$is_new', " .
                "is_hot = '$is_hot', " .
                "is_on_sale = '$is_on_sale', " .
                "is_alone_sale = '$is_alone_sale', " .
                "is_shipping = '$is_shipping', " .
                "goods_desc = '$_POST[goods_desc]', " .
                "last_update = '". gmtime() ."', ".
                "goods_type = '$goods_type' " .
                "WHERE goods_id = '$_REQUEST[goods_id]' LIMIT 1";
    }
    */
    if ($is_insert){
     if ($code == ''){
      $sql = "INSERT INTO " . $ecs->table('goods') . " (goods_name, goods_name_style, goods_sn, " .
          "cat_id, brand_id, shop_price, market_price, is_promote, promote_price, " .
             "promote_start_date, promote_end_date, goods_img, goods_thumb, original_img,keywords, goods_brief, " .
             "seller_note, goods_weight, goods_number, warn_number,max_number, integral,give_integral, is_best, is_new, is_hot, ".
             "is_on_sale, is_alone_sale, goods_desc, add_time, last_update, goods_type,rank_integral)" .
            "VALUES ('$_POST[goods_name]', '$goods_name_style', '$goods_sn', '$catgory_id', " .
             "'$brand_id', '$shop_price', '$market_price', '$is_promote','$promote_price', ".
             "'$promote_start_date', '$promote_end_date', '$goods_img', '$goods_thumb', '$original_img', ".
             "'$_POST[keywords]', '$_POST[goods_brief]', '$_POST[seller_note]', '$goods_weight','$goods_number',".
             "'$warn_number','$max_number', '$_POST[integral]', '$give_integral', '$is_best', '$is_new', '$is_hot', '$is_on_sale', '$is_alone_sale', ".
             "'$_POST[goods_desc]', '" . gmtime() . "', '". gmtime() ."', '$goods_type','$rank_integral')";
     }else{
      $sql = "INSERT INTO " . $ecs->table('goods') . " (goods_name, goods_name_style, goods_sn, ".
         "cat_id, brand_id, shop_price, market_price, is_promote, promote_price, " .
      "promote_start_date, promote_end_date, goods_img, goods_thumb, original_img,keywords, goods_brief, " .
      "seller_note, goods_weight, goods_number, warn_number, max_number,integral,give_integral, is_best, is_new, is_hot, is_real, " .
      "is_on_sale, is_alone_sale, goods_desc, add_time, last_update, goods_type,extension_code, rank_integral)" .
      "VALUES ('$_POST[goods_name]', '$goods_name_style', '$goods_sn', '$catgory_id', " .
      "'$brand_id', '$shop_price', '$market_price', '$is_promote','$promote_price', ".
      "'$promote_start_date', '$promote_end_date', '$goods_img', '$goods_thumb','$original_img', ".
      "'$_POST[keywords]', '$_POST[goods_brief]', '$_POST[seller_note]', '$goods_weight','$goods_number',".
      " '$warn_number','$max_number', '$_POST[integral]', '$give_integral', '$is_best','$is_new', '$is_hot', 0, '$is_on_sale', '$is_alone_sale’' ".
      " '$_POST[goods_desc]', '" . gmtime() . "', '". gmtime() ."', '$goods_type', '$code', '$rank_integral')";
     }
    }else{

      $sql = "SELECT goods_thumb, goods_img, original_img " .
             " FROM " . $ecs->table('goods') .
       " WHERE goods_id = '$_REQUEST[goods_id]'";
      $row = $db->getRow($sql);
      if ($proc_thumb && $goods_img && $row['goods_img'] &&
      !goods_parse_url($row['goods_img']))
      {
       @unlink(ROOT_PATH . $row['goods_img']);
       @unlink(ROOT_PATH . $row['original_img']);
      }
      if ($proc_thumb && $goods_thumb && $row['goods_thumb'] && !goods_parse_url($row['goods_thumb']))
      {
       @unlink(ROOT_PATH . $row['goods_thumb']);
      }
       $sql = "UPDATE " . $ecs->table('goods') . " SET " .
       "goods_name = '$_POST[goods_name]', " .
       "goods_name_style = '$goods_name_style', " .
       "goods_sn = '$goods_sn', " .
       "cat_id = '$catgory_id', " .
       "brand_id = '$brand_id', " .
       "shop_price = '$shop_price', " .
       "market_price = '$market_price', " .
       "is_promote = '$is_promote', " .
       "promote_price = '$promote_price', " .
       "promote_start_date = '$promote_start_date', " .
       "promote_end_date = '$promote_end_date', ";

       if ($goods_img)
       {
        $sql .= "goods_img = '$goods_img', original_img = '$original_img', ";
       }
       if ($goods_thumb)
       {
        $sql .= "goods_thumb = '$goods_thumb', ";
       }
       if ($code != '')
       {
        $sql .= "is_real=0, extension_code='$code', ";
       }
       $sql .= "keywords = '$_POST[keywords]', " .
        "goods_brief = '$_POST[goods_brief]', " .
        "seller_note = '$_POST[seller_note]', " .
        "goods_weight = '$goods_weight'," .
        "goods_number = '$goods_number', " .
        "warn_number = '$warn_number', " .
        "max_number = '$max_number', " .
        "integral = '$_POST[integral]', " .
        "give_integral = '$give_integral', " .
        "rank_integral = '$rank_integral', " .
        "is_best = '$is_best', " .
        "is_new = '$is_new', " .
        "is_hot = '$is_hot', " .
        "is_on_sale = '$is_on_sale', " .
        "is_alone_sale = '$is_alone_sale', " .
        "goods_desc = '$_POST[goods_desc]', " .
        "last_update = '". gmtime() ."', ".
        "goods_type = '$goods_type' " .
        "WHERE goods_id = '$_REQUEST[goods_id]' LIMIT 1";
     }

步驟三:修改後台模板文件(在市場價會員價下面新增一個設置最大訂購數量的欄位)
開啟admin/templates/goods_info.htm,找到

   <tr>
       <td class="label">{$lang.lab_shop_price}</td>
       <td><input type="text" name="market_price" value="{$goods.shop_price}" size="20" />
       <input type="button" value="{$lang.integral_market_price}" onclick="integral_market_price()" />{$lang.require_field}</td>
   </tr>

在其後面新增下列語法

<!-- 商品最大訂購數量 start -->
    <tr>
        <td class="label">{$lang.lab_max_number}</td>
        <td><input type="text" name="max_number" value="{$goods.max_number}" size="20" />
           <span class="notice-span" {if $help_open}style="display:block" {else} style="display:none" mce_style="display: none;" {/if} id="minNumber">{$lang.notice_max_number}</span>
        </td>
        </td>
    </tr>
<!-- 商品最大訂購數量 end -->

步驟四:修改語系檔
開啟languages/zh_tw/admin/goods.php

在最後一行新增

$_LANG['lab_max_number']       = '最大訂購數量:';
$_LANG['notice_max_number']    = '該商品最大訂購數量,0表示無限制。'

步驟五:修改前台模版設置
下面以ecshop預設的default模板為例
開啟themes/default/goods.dwt,大約在284行的位置找到下列語法

<input name="number" type="text" id="number" value="1" size="4" onBlur="changePrice()" style="border:1px solid #ccc; " mce_style="border: 1px solid #ccc;"/>

改為

<input name="number" type="text" id="number" value="{$goods.max_number|default:"1"}" size="4" onblur="changePrice()" style=”border:1px solid #ccc; " mce_style="border: 1px solid #ccc;" />

大約在550行的位置找到

var qty = document.forms['ECS_FORMBUY'].elements['number'].value;

在其後新增下列語法

{if $goods.max_number}
if(qty < {$goods.max_number})
{
alert('您訂購的數量已超出此商品的最大訂購數量:{$goods.max_number}');
qty = {$goods.max_number};
}
{/if}

步驟六:修改購物車的商品數量判斷
開啟flow.php,大約在1800行的位置找到

/*------------------------------------------------------ */
//-- 更新购物车
/*------------------------------------------------------ */

elseif ($_REQUEST['step'] == 'update_cart')
{
    if (isset($_POST['goods_number']) && is_array($_POST['goods_number']))
    {
        flow_update_cart($_POST['goods_number']);
    }

    show_message($_LANG['update_cart_notice'], $_LANG['back_to_cart'], 'flow.php');
    exit;
}

改為

/*------------------------------------------------------ */
//-- 更新购物车
/*------------------------------------------------------ */
/*
elseif ($_REQUEST['step'] == 'update_cart')
{
    if (isset($_POST['goods_number']) && is_array($_POST['goods_number']))
    {
        flow_update_cart($_POST['goods_number']);
    }

    show_message($_LANG['update_cart_notice'], $_LANG['back_to_cart'], 'flow.php');
    exit;
}*/

elseif ($_REQUEST['step'] == 'update_cart')
{
    //查询:
    echo '$_POST["check_goods_id"]=' . $_POST['check_goods_id'] . "<br />";
    echo '$_POST["check_goods_number"]=' . $_REQUEST['check_goods_number'] . "<br />";
    echo '$_POST["goods_number"]=' . $_POST['goods_number'] . "<br />";
    $sql = "SELECT max_number FROM " .$GLOBALS['ecs']->table('goods'). " WHERE goods_id = " . $_POST['check_goods_id'];
    $row = $GLOBALS['db']->getRow($sql);
    if (isset($_POST['goods_number']) && is_array($_POST['goods_number']))
    {
        if ($row['max_number'] > 0 && $_REQUEST['check_goods_number'] > $row['max_number'])
        {
            show_message(sprintf($GLOBALS['_LANG']['max_number_insufficiency'], $row['goods_name'],
            $row['max_number'], $row['max_number']));                
            exit;
        }
        flow_update_cart($_POST['goods_number']);
    }

    show_message($_LANG['update_cart_notice'], $_LANG['back_to_cart'], 'flow.php');
    exit;
}

步驟七:修改前台模版設置
下面以ecshop預設的default模板為例
開啟themes/default/flow.dwt,大約在90行的位置找到下列語法

<!-- {if $goods.goods_id gt 0 && $goods.is_gift eq 0 && $goods.parent_id eq 0} 普通商品可修改数量 -->
<input type="text" name="goods_number[{$goods.rec_id}]" id="goods_number_{$goods.rec_id}" value="{$goods.goods_number}" size="4" class="inputBg" style="text-align:center " onkeydown="showdiv(this)"/>
<!-- {else} -->

修改為:

<!-- {if $goods.goods_id gt 0 && $goods.is_gift eq 0 && $goods.parent_id eq 0} 普通商品可修改数量 -->
<input onchange="chggdnumber('goods_number_{$goods.rec_id}');" type="text" name="goods_number[{$goods.rec_id}]" id="goods_number_{$goods.rec_id}" value="{$goods.goods_number}" size="4" class="inputBg" style="text-align:center" onkeydown="showdiv(this)"  />
<!-- {else} -->

接著大約在127行的位置找到下列語法

<!-- {if $smarty.session.user_id gt 0} -->

修改為:

<script type="text/javascript">
function chggdnumber(id)
{
var sdoc;
var hdoc;
var keynum;
sdoc = document.getElementById(id);
hdoc = document.getElementById('check_goods_number');
keynum = sdoc.value;
hdoc.value = keynum;                        
}      
</script>
<!-- {if $smarty.session.user_id gt 0} -->

步驟八:
開啟languages/zh_tw/shopping_flow.php,大約在36行的位置找到

$_LANG['stock_insufficiency'] = '非常抱歉,您選擇的商品 %s 的庫存數量只有 %d,您最多只能購買 %d 件。';

在其後新增一行

$_LANG['max_number_insufficiency'] = '非常抱歉,您選擇的商品 %s 訂購數量已超出可訂購的最大限制 %d,您最多只能購買 %d 件。';

把修改過的檔案全部上傳,收工。

PS1.
此修改是以下面這篇修改為基礎,將最小購買數量限制反向改為最大購買數量限制,並加上購物車內容(flow.php)的數量修改判斷:
http://bbs.ecshop.com/viewthread.php?tid=160485

PS2.
當客戶在購物車頁面更新商品訂購數量時,如果超過最大訂購數量限制,系統會提示訂購數量已超出可訂購的最大限制的警告並回到購物車畫面,但此時數量仍然會顯示客戶所輸入的錯誤數量,不過不論是此時重新整理畫面或直接進入結帳流程,商品數量都會以最大訂購數量顯示並進行結帳。

Tags: , , , , , ,

發佈日期: 2012年03月27日 星期二 | 文章分類:ECSHOP | 訂閱這篇文章:RSS 2.0
Both comments and pings are currently closed.

Comments are closed.