Ecshop 限制购买数量

2016-07-07 14:55 来源:www.chinab4c.com 作者:ecshop专家

做过商城,或者说经营过商城的都会知道,有很多商品利润很小,而且还要包运费什么的,所以根本就是做亏本的生意,如果利润小,数量多的话这样就不会出现赔本的损失。所以 今天第一个插件也是很实用的插

件就是如果在后台设置商品最小起订量



第一步,商品表必须有个字段代表某个商品最小订购数量->min_number

打开goods表在最后字段添加一个min_numbertinyint类型默认值为0代表没有最小起订量

sql:alter table `goods` add column `min_number` tinyint (3) UNSIGNEDDEFAULT '0' NOT NULLafter `warn_number`

第二部,在后台添加商品的时候 必须有个text文本框设置最小起订量

admin/goods.php

在'warn_number'=> 1 , 代码行后插入'min_number'=> 0,

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

后添加代码$min_number = isset($_POST['min_number']) ? $_POST['min_number'] : 0;

第三部 修改添加商品sql语句

修改代码

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,min_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','$min_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, min_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','$min_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', " .
"min_number = '$min_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>



添加
<!-- 新增 批发数量限制 begin -->
<tr>
<td class="label">{$lang.lab_min_number}</td>
<td><input type="text" name="min_number" value="{$goods.min_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_min_number}</span>
</td>
</td>
</tr>
<!-- 批发数量限制 end -->



第五步:修改语言languages/zh_cn/admin/goods.php

最后添加
$_LANG['lab_min_number'] = '最小购买数量:';
$_LANG['notice_min_number'] = '该商品最小购买数量,0表示按购买数量无限制.'

到现在位置 后台设置全部结束了,不知道大家能懂了没有



下面我门说说前台如何来实现

我以default模板为例子给大家讲解

想做起订量限制,这个必然是在商品购买页面购买数量框来做限制最简单的办法就是用JS当然还有别的办法

首先给那些如果给了起订量的商品购买页面直接显示默认最少购买数量

如果在后台设置的AA商品最少5个 那么进入AA商品页面购买数量默认值就是5

我这样来实现

修改themes/default/goods.dwt
<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.min_number|default:"1"}" size="4" onblur="changePrice()" style=”border:1px solid #ccc; " mce_style="border: 1px solid #ccc;"/><br

/>
这样就是value也就是默认值了



最后添加JS判断就好了

themes/default/goods.dwt


var qty = document.forms['ECS_FORMBUY'].elements['number'].value;
后添加
{if $goods.min_number}
if(qty < {$goods.min_number})
{
alert('您输入的数字小于此商品最小购买数量:{$goods.min_number}');
qty = {$goods.min_number};
}
{/if}

回答:
思路是对的。呵呵

这个功能很实用

还缺呢,这样做是不完整的,购买涉及的页面不仅是商品页

很棒的修改,但當客戶在購物車畫面修改訂購數量時並沒有判斷,以下是我自己改的

開啟flow.php,大約在2195行的位置找到
  1. $sql = "SELECT g.goods_name, g.goods_number ".
  2. "FROM " .$GLOBALS['ecs']->table('goods'). " AS g, ".
  3. $GLOBALS['ecs']->table('cart'). " AS c ".
  4. "WHERE g.goods_id = c.goods_id AND c.rec_id = '$key'";
  5. $row = $GLOBALS['db']->getRow($sql);

  6. //查询:系统启用了库存,检查输入的商品数量是否有效
  7. if (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] != 'package_buy')
  8. {
  9. if ($row['goods_number'] < $val)
  10. {
  11. show_message(sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'],
  12. $row['goods_number'], $row['goods_number']));
  13. exit;
  14. }
复制代码

改為
  1. $sql = "SELECT g.goods_name, g.goods_number, g.max_number ".
  2. "FROM " .$GLOBALS['ecs']->table('goods'). " AS g, ".
  3. $GLOBALS['ecs']->table('cart'). " AS c ".
  4. "WHERE g.goods_id = c.goods_id AND c.rec_id = '$key'";
  5. $row = $GLOBALS['db']->getRow($sql);

  6. //查詢:系統啟用了庫存,檢查輸入的商品數量是否有效
  7. if (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] != 'package_buy')
  8. {
  9. if ($row['max_number'] > 0 && $row['goods_number'] > $row['max_number'])
  10. {
  11. show_message(sprintf($GLOBALS['_LANG']['max_number_insufficiency'], $row['goods_name'],
  12. $row['max_number'], $row['max_number']));
  13. exit;
  14. }
  15. elseif ($row['goods_number'] < $val)
  16. {
  17. show_message(sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'],
  18. $row['goods_number'], $row['goods_number']));
  19. exit;
  20. }
复制代码

開啟languages/zh_tw/shopping_flow.php,大約在36行的位置找到
  1. $_LANG['stock_insufficiency'] = '非常抱歉,您選擇的商品 %s 的庫存數量只有 %d,您最多只能購買 %d 件。';
复制代码

在其後新增一行
  1. $_LANG['max_number_insufficiency'] = '非常抱歉,您選擇的商品 %s 訂購數量已超出可訂購的最大限制 %d,您最多只能購買 %d 件。';
复制代码


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

限制购买数量 标记

我也很想知道怎么弄的。好像蛮复杂的。哎!!

  1. if($row['max_number'] >0 && $val > $row['max_number']){

  2. show_message(sprintf($GLOBALS['_LANG']['max_number_insufficiency'], $row['goods_name'],
  3. $row['max_number'], $row['max_number']));
  4. exit;
  5. }
复制代码
回复 5# blakechiang
有错误 关键逻辑应该是


回复blakechiang 有错误 关键逻辑应该是
odayou 发表于 2013-8-9 19:09

因为用户可能后退浏览器,然后再次加入购物车,因此我再给大家增加最后一关:下单时候的验证
1.在/flow.php最后加上如下函数
  1. /**检查订单中是否有超出购买数量的商品**/
  2. function get_maxMumber(array $orderlist){

  3. $html = "非常抱歉,您的购物车遇到以下问题需要确认:" ;
  4. $i=0;
  5. foreach($orderlist as $key => $value){
  6. if(is_array($value)){

  7. $sql="select max_number from ". $GLOBALS['ecs']->table('goods') ." where goods_id='".$value['goods_id']."'";

  8. $re = $GLOBALS['db']->getRow($sql,false);
  9. if($re['max_number'] >0 && $value['goods_number'] > $re['max_number'] ){
  10. $html .= sprintf("<br/>&nbsp;&nbsp;商品 %s 的限购数量为 %d ,您的购物车中有 %d 个", $value['goods_name'],$re['max_number'], $value['goods_number']);
  11. $i++;
  12. }


  13. }
  14. }

  15. if($i == 0){
  16. return false;
  17. }else{
  18. return $html;
  19. }

  20. }
复制代码


2.找到/flow.php的 elseif ($_REQUEST['step'] == 'done')这一步逻辑中的代码:
  1. /* 插入订单表 */
  2. $error_no = 0;
  3. do
  4. {
复制代码


3.在此处,增加
  1. //订单中存在超出限购数量的商品
  2. if($html = get_maxMumber($cart_goods)){
  3. show_message($html, '修改购物车', 'flow.php');
  4. exit;
  5. }
复制代码
则可最后把关。不过这一步要是能加在/flow.php?step=checkout这一步就更好了
有更好办法的可以交流下qq:726331595

非常有用的东西,受教了,感谢!


欧码噶,这个确实又有漏洞 现在发现当商品有多个属性的时候 每个属性买几件就超出了购买限度 而我们的判断逻辑完全没考虑到这里>>>> 求救 求指导



我又简单的做了下,现在可以处理不同属性的同一商品数量问题,更改上上上楼的get_maxMumber函数为
  1. /**检查订单中是否有超出购买数量的商品**/
  2. function get_maxMumber(array $orderlist){
  3. $temparray = array();
  4. $html = "非常抱歉,您遇到以下问题需要更改购物车:" ;
  5. $i=0;


  6. //处理同一商品的不同属性共存问题
  7. foreach($orderlist as $good){
  8. if(array_key_exists($good['goods_id'],$temparray)){
  9. $temparray[$good['goods_id']]['goods_number'] += $good['goods_number'];
  10. $temparray[$good['goods_id']]['goods_attr'] .= "+".$good['goods_attr'];
  11. $temparray[$good['goods_id']]['subtotal'] += $good['subtotal'];
  12. }else{
  13. $temparray[$good['goods_id']] = $good;
  14. }
  15. }

  16. foreach($temparray as $key => $value){
  17. if(is_array($value)){

  18. $sql="select max_number from ". $GLOBALS['ecs']->table('goods') ." where goods_id='".$value['goods_id']."'";

  19. $re = $GLOBALS['db']->getRow($sql,false);
  20. if($re['max_number'] >0 && $value['goods_number'] > $re['max_number'] ){
  21. $i++;
  22. $html .= sprintf("<br/>".$i.". 商品 %s 的限购数量为 %d ,您的购物车中有 %d 件", $value['goods_name'],$re['max_number'], $value['goods_number']);

  23. }

  24. }
  25. }

  26. if($i == 0){
  27. return false;
  28. }else{
  29. return $html;
  30. }

  31. }
复制代码



暂时更新购物车那个动作还没做到两个商品一起处理