张文保的博客
张文保 · Paul Zhang

Engineering the Future
功不唐捐,玉汝于成

ECSHOP

ECSHOP给商品设置最少购买数量的方法

ecshop后台默认没有给商品设置最小起订量或者最少购买量的功能。保哥在使用ecshop做一个商城时,要给商品设置包邮来吸引买家,但又不想买家购买一件产品也给包邮,这样很容易亏本。对于价值较高的商品有一定的利润空间设置包邮是合情合理的,但是有的商品价格都低于运费了,就不能再包邮了。有的朋友可能会说,可以发布商品属性价格,不同数量装的价格或者发布商品时,设置十个装的价格,但是这样的话,买家一眼看到商品上显示的价位太高,可能心理上就不愿意再看了。我们设置一件商品的价格,这样在心理上感觉便宜。具体的解决思路是可以给ecshop在下订单设置一个强制提示最小购买数量。具体的解决步骤如下:

编辑/themes/goods.dwt文件,找到以下代码:

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

 

替换为以下代码:

<input type="text" id="number" size="4" value="10" style="border:1px solid #ccc; " onkeyup="if(this.value.length > 1 && /^[1-9]d+$/.test(this.value) == false){alert('输入有误,购买数量不少于10个')}" onblur="if(this.value.length == 1)alert('输入有误,购买数量不少于10个');changePrice()"/>

 

但是这种方法会使得全站所有的商品购买数量都设置为不低于十个,不太人性化。那么下面这种修改数据库的方法,可以在后台产品编辑时,设置每个商品的最小购买数量。具体方法如下:

第一步,给商品表创建一个新字段min_number用于储存每个商品的最小订购数量数据,打开goods表在最后字段添加一个min_number类型为tinyint默认值为0

alter table 'goods' add column 'min_number' tinyint (3) UNSIGNED DEFAULT '0' NOT NULL after '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模板为例,修改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;"/>

 

这样就是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}

 

相关文章
本文标题:《ECSHOP给商品设置最少购买数量的方法》
网址:https://zhangwenbao.com/ecshop-sets-the-minimum-number-of-purchases-for-goods.html
作者:张文保
发布时间:2017-01-25
许可协议:CC BY-NC-SA 4.0
发表新评论
SSL安全认证