Ecshop模板网
首页 > ecshop教程 > ecshop经验分享 > Ecshop实现仿Taobao地区运费模板
文章详情

Ecshop实现仿Taobao地区运费模板

ecshop模板网 / 2014-05-07


    淘宝网(Taobao)购物的宝贝详情页面,可以针对不同地区显示不同运费,运费由后台设定;结算时间,按重量、件数计算运费。Ecshop本身有配送方式插件,已有多家物流公司插件,例如:顺丰快递、申通快递、圆通快递等。本文介绍如何实现按地区显示运费,并且让每个商品绑定运费模板。

     1、Ecshop后台配送方式创建

      进入Ecshop后台"系统设置-->配送方式",将“顺丰快递”改名称为“粮食快递”,配送ID号为6。

     2、商品绑定配送方式的运费模板

       2.1 数据表“ecs_goods”增加一个字段,执行下面SQL语句:


1 ALTER TABLE  `ecs_goods` ADD `shipping_id` MEDIUMINT(9) NOT&nbsNULLbsp;DEFAULT '6';

        2.2 后台添加/编辑 商品 调出已经安装配送方式 "admin/ goods.php ",将此shipping_list函数添加到goods.php最末处。


01 /**
02  * 取得已安装的配送方式
03  * @return  array   已安装的配送方式
04 */
05 function shipping_list()
06 {
07     $sql = 'SELECT shipping_id, shipping_name ' .
08             'FROM ' . $GLOBALS['ecs']->table('shipping') .
09             ' WHERE enabled = 1';
10  
11     return $GLOBALS['db']->getAll($sql);
12 }

      在代码前“$smarty->assign('unit_list', get_unit_list());”增加调用代码

1 // LONGHTML 增加运费模板
2 $smarty->assign('shipping_list', shipping_list());
3 // END
4 $smarty->assign('unit_list', get_unit_list());
      在“/* 处理商品数据 */”后面,增加POST过来的“shipping_id ”表单值进行赋值 
1 /* 处理商品数据 */
2  
3 // LONGHTML 运费模板(新增,更新)
4 $shipping_id = empty($_POST['shipping_id']) ? '0' : intval($_POST['shipping_id']);
5 // END

     最后一步是“插入/更新”商品时,对“shipping_id”字段实现处理。直接替换掉下面代码


01 /* 入库 */
02     if ($is_insert)
03     {
04         if ($code == '')
05         {
06             $sql = "INSERT INTO " . $ecs->table('goods') . " (goods_name, goods_name_style, goods_sn, " .
07                     "cat_id, brand_id, shop_price, logi_cost, market_price, is_promote, promote_price, " .
08                     "promote_start_date, promote_end_date, goods_img, index_img, goods_thumb, original_img, keywords, goods_brief, " .
09                     "seller_note, goods_weight, goods_number, warn_number, integral, give_integral, is_best, is_new, is_hot, " .
10                     "is_on_sale, is_alone_sale, is_shipping, goods_desc, add_time, last_update, goods_type, rank_integral, suppliers_id, province, city, virtual_buy,shipping_id)" .
11                 "VALUES ('$_POST[goods_name]', '$goods_name_style', '$goods_sn', '$catgory_id', " .
12                     "'$brand_id', '$shop_price', '$logi_cost', '$market_price', '$is_promote','$promote_price', ".
13                     "'$promote_start_date', '$promote_end_date', '$goods_img', '$index_img', '$goods_thumb', '$original_img', ".
14                     "'$_POST[keywords]', '$_POST[goods_brief]', '$_POST[seller_note]', '$goods_weight', '$goods_number',".
15                     " '$warn_number', '$_POST[integral]', '$give_integral', '$is_best', '$is_new', '$is_hot', '$is_on_sale', '$is_alone_sale', $is_shipping, ".
16                     " '$_POST[goods_desc]', '" . gmtime() . "', '". gmtime() ."', '$goods_type', '$rank_integral', '$suppliers_id', '$goods_provincestr', '$goods_citystr', '$virtual_buy', '$shipping_id' )";
17         }
18         else
19         {
20             $sql = "INSERT INTO " . $ecs->table('goods') . " (goods_name, goods_name_style, goods_sn, " .
21                     "cat_id, brand_id, shop_price, logi_cost, market_price, is_promote, promote_price, " .
22                     "promote_start_date, promote_end_date, goods_img, index_img, goods_thumb, original_img, keywords, goods_brief, " .
23                     "seller_note, goods_weight, goods_number, warn_number, integral, give_integral, is_best, is_new, is_hot, is_real, " .
24                     "is_on_sale, is_alone_sale, is_shipping, goods_desc, add_time, last_update, goods_type, extension_code, rank_integral, province, city, virtual_buy,shipping_id)" .
25                 "VALUES ('$_POST[goods_name]', '$goods_name_style', '$goods_sn', '$catgory_id', " .
26                     "'$brand_id', '$shop_price', '$logi_cost', '$market_price', '$is_promote','$promote_price', ".
27                     "'$promote_start_date', '$promote_end_date', '$goods_img', '$index_img', '$goods_thumb', '$original_img', ".
28                     "'$_POST[keywords]', '$_POST[goods_brief]', '$_POST[seller_note]', '$goods_weight', '$goods_number',".
29                     " '$warn_number', '$_POST[integral]', '$give_integral', '$is_best', '$is_new', '$is_hot', 0, '$is_on_sale', '$is_alone_sale', $is_shipping, ".
30                     " '$_POST[goods_desc]', '" . gmtime() . "', '". gmtime() ."', '$goods_type', '$code', '$rank_integral', '$goods_provincestr', '$goods_citystr', '$virtual_buy', '$shipping_id')";
31         }
32     }
33     else
34     {
35         /* 如果有上传图片,删除原来的商品图 */
36         $sql = "SELECT goods_thumb, goods_img, index_img, original_img " .
37                     " FROM " . $ecs->table('goods') .
38                     " WHERE goods_id = '$_REQUEST[goods_id]'";
39         $row = $db->getRow($sql);
40         if ($proc_thumb && $goods_img && $row['goods_img'] && !goods_parse_url($row['goods_img']))
41         {
42             @unlink(ROOT_PATH . $row['goods_img']);
43             @unlink(ROOT_PATH . $row['original_img']);
44         }
45  
46         if ($proc_thumb && $goods_thumb && $row['goods_thumb'] && !goods_parse_url($row['goods_thumb']))
47         {
48             @unlink(ROOT_PATH . $row['goods_thumb']);
49         }
50          
51         if ($index_img && $row['index_img'] && !goods_parse_url($row['index_img']))
52         {
53             @unlink(ROOT_PATH . $row['index_img']);
54         }
55  
56         $sql = "UPDATE " . $ecs->table('goods') . " SET " .
57                 "goods_name = '$_POST[goods_name]', " .
58                 "goods_name_style = '$goods_name_style', " .
59                 "goods_sn = '$goods_sn', " .
60                 "cat_id = '$catgory_id', " .
61                 "brand_id = '$brand_id', " .
62                 "shop_price = '$shop_price', " .
63                 "logi_cost = '$logi_cost', " .
64                 "market_price = '$market_price', " .
65                 "is_promote = '$is_promote', " .
66                 "promote_price = '$promote_price', " .
67                 "promote_start_date = '$promote_start_date', " .
68                 "suppliers_id = '$suppliers_id', " .
69                 "province = '$goods_provincestr', " .
70                 "city = '$goods_citystr', " .
71                 "virtual_buy = '$virtual_buy', " .
72                 "shipping_id = '$shipping_id', " .
73                 "promote_end_date = '$promote_end_date', ";
74  
75         /* 如果有上传图片,需要更新数据库 */

      2.3 后台添加/编辑商品 实现绑定配送方式"admin/goods_info.htm"

1 <tr>
2        <td class="label">运费模板</td>
3        <td><sel ect name="shipping_id" ><option value="0">{$lang.sel ect_please}
4            {foreach from=$shipping_list item=shipping}
5              <option value="{$shipping.shipping_id}" {if $shipping.shipping_id eq $goods.shipping_id}sel ected{/if}>{$shipping.shipping_name}</option>
6            {/foreach}
7     </sel ect>{$lang.require_field}</td>
8      </tr>

    在品牌下面,增加绑定运费模板。效果如下:

      3、前台商品详情调用设置好的配送方式

      以主题default为例,增加新文件:
          1、chrome.js (themes/default/js)
          2、icon_2.jpg (themes/default/images)

     goods.php页面商品显示部分加入调用代码

01 /***** 商品页按地区显示运费 ***********************************************************************/
02  $shippings = array();
03  $res = $db->GetAll("SELECT shipping_name, shipping_id FROM ecs_shipping WHERE shipping_id=".$goods['shipping_id']);
04  foreach ($res as $value)
05  {
06  $areas = array();
07  $res1 = $db->GetAll("SELECT * FROM ecs_shipping_area WHERE shipping_id = $value[shipping_id]");
08  foreach ($res1 as $area)
09  {
10  $configure = unserialize($area['configure']);
11  if (is_array($configure))
12  {
13  foreach ($configure as $c)
14  {
15  if ($c['name'] == 'base_fee')
16  {
17  $price = $c['value'];
18  }
19  }
20  }
21  $sql = "SELECT a.region_id, r.region_name ".
22  "FROM ".$ecs->table('area_region')." AS a, ".$ecs->table('region'). " AS r ".
23  "WHERE r.region_id=a.region_id AND a.shipping_area_id='$area[shipping_area_id]'";
24  $res2 = $db->query($sql);
25  while ($arr = $db->fetchRow($res2))
26  {
27  $value['areas'][$arr['region_name']] = $price;
28  }
29  }
30  $shippings[] = $value;
31  }
32  $res = $db->GetAll("SELECT region_id,region_name FROM ecs_region WHERE parent_id = 1");
33  if($goods['shipping_id'] == 6)
34  {
35  $current_region = '广东';   //默认显示广东省
36  $smarty->assign('current_region',   $current_region);
37  $smarty->assign('current_price',   '7');
38  }
39  
40  foreach ($res as $value)
41  {
42  $row = array();
43  foreach ($shippings as $a => $shipping)
44  {
45  if ($shipping['areas'])
46  {
47  foreach ($shipping['areas'] as $key => $price)
48  {
49  if ($key == $value['region_name'])
50  {
51  $row[$a]['shipping_price'] = $price;
52  }
53  }
54  }
55  if ($row[$a]['shipping_price'] > 0)
56  {
57  $row[$a]['shipping_name'] = $shipping['shipping_name'];
58  $value['shippings'] = $row;
59  }
60  }
61  if ($value['shippings']) $regions[] = $value;
62  }
63  $smarty->assign('regions',              $regions);
64  /****************************************************************************/

            goods.dwt  加在需要显示运费的地方

01  <!--{if $regions}-->
02  <script src="themes/yihaodian/js/chrome.js" type="text/javascript"></script>
03  {foreach from=$regions key=key item=value}
04  {if $key == 0}
05  <p id="chromemenu">至 <a rel="dropmenu1" href="javascript:;"><b id="s_a_name">{$current_region}</b><img style="margin:0 2px 0 2px;" src="images/icon_2.jpg" align="absmiddle" /></a>:<b id="s_a_price">
06  {foreach from=$value.shippings item=shipping}
07  {$shipping.shipping_name}{$current_price}元 &nbsp;
08  {/foreach}
09  </b>
10  </p>
11  {/if}
12  {/foreach}
13  <div id="dropmenu1" class="dropmenudiv">
14  {foreach from=$regions item=value}
15  <a href="javascript:;" onclick="show_shipping('{$value.region_name}','{foreach from=$value.shippings item=shipping}{$shipping.shipping_name}{$shipping.shipping_price}元 &nbsp;{/foreach}')">{$value.region_name}</a>
16  {/foreach}
17  </div>
18  <script>
19  function show_shipping(name,price)
20  {
21  document.getElementById("s_a_name").innerHTML = name;
22  document.getElementById("s_a_price").innerHTML = price;
23  }
24  cssdropdown.startchrome("chromemenu");
25  </script>
26  <style>
27  #chromemenu b { font-weight:normal}
28  .dropmenudiv {position:absolute;top: 0;z-index:100;width:200px;visibility: hidden; background:#fdffee; padding:8px; border:solid #ffbf69 2px; line-height:25px;}
29  .dropmenudiv a { margin:0 5px 0 5px;}
30  </style>
31  <!--{/if}-->

  前台显示最终效果图,默认广东省

     4、结算流程中,根据配送地址计算运费

        4.1 重写“include/lib_order.php”中last_shipping_and_payment函数。多个商品,不同配送方式,调用配送方式ID,以最贵配送方式计算。买家可以找客服进行,运费改价。

01 /**
02  * 获得上一次用户采用的支付和配送方式
03  *
04  * @access  public
05  * @return  void
06  */
07 function last_shipping_and_payment()
08 {
09     $sql = "SELECT shipping_id, pay_id " .
10             " FROM " . $GLOBALS['ecs']->table('order_info') .
11             " WHERE user_id = '$_SESSION[user_id]' " .
12             " ORDER BY order_id DESC LIMIT 1";
13     $row = $GLOBALS['db']->getRow($sql);
14  
15     /* LONGHTML 获得购物车中商品 运费模板最大值 */
16     $sql = "SELECT DISTINCT max(g.shipping_id) as  shipping_id " .
17             " FROM " . $GLOBALS['ecs']->table('cart') ." AS c ".
18             " LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g ON c.goods_id = g.goods_id" .
19             " WHERE c.`session_id` =  '" . SESS_ID . "'".
20             " AND c.`extension_code` !=  'package_buy' ";
21     $shipping_id = $GLOBALS['db']->getOne($sql);
22     $row['shipping_id'] = $shipping_id;
23     // END
24  
25     if (empty($row))
26     {
27         /* 如果获得是一个空数组,则返回默认值 */
28         $row = array('shipping_id' => 0, 'pay_id' => 0);
29     }
30  
31     return $row;
32 }
    4.2  flow.php购物流程checkout,done步骤,调用商品绑定的配送方式
01 /* 对是否允许修改购物车赋值 */
02 if ($flow_type != CART_GENERAL_GOODS || $_CFG['one_step_buy'] == '1')
03 {
04     $smarty->assign('allow_edit_cart', 0);
05 }
06 else
07 {
08     $smarty->assign('allow_edit_cart', 1);
09 }
10  
11 // LONGHTML 最大值的运费模板
12 $arr = last_shipping_and_payment();
13 $_SESSION['flow_order']['shipping_id'] = $arr['shipping_id'];
14 $smarty->assign('sel ect_shipping_id', $arr['shipping_id']);
15 // END
01 /* 检查收货人信息是否完整 */
02 if (!check_consignee_info($consignee, $flow_type))
03 {
04     /* 如果不完整则转向到收货人信息填写界面 */
05     ecs_header("Location: flow.php?step=consignee\n");
06     exit;
07 }
08  
09 $_POST['how_oos'] = isset($_POST['how_oos']) ? intval($_POST['how_oos']) : 0;
10 $_POST['card_message'] = isset($_POST['card_message']) ? htmlspecialchars($_POST['card_message']) : '';
11 $_POST['inv_type'] = !empty($_POST['inv_type']) ? htmlspecialchars($_POST['inv_type']) : '';
12 $_POST['inv_payee'] = isset($_POST['inv_payee']) ? htmlspecialchars($_POST['inv_payee']) : '';
13 $_POST['inv_content'] = isset($_POST['inv_content']) ? htmlspecialchars($_POST['inv_content']) : '';
14 $_POST['postscript'] = isset($_POST['postscript']) ? htmlspecialchars($_POST['postscript']) : '';
15  
16 // LONGHTML 最大值的运费模板
17 $arr = last_shipping_and_payment();
18 $_SESSION['flow_order']['shipping_id'] = $arr['shipping_id'];
19 // END

  将themes/default/flow.dwt配送方式隐藏掉

01 <!--{if $total.real_goods_count neq 0}-->
02     <div class="" style="display:none;">
03     <h5><span>{$lang.shipping_method}</span></h5>
04     <table width="984" align="center" border="0" cellpadding="5" cellspacing="1"bgcolor="#dddddd" id="shippingTable">
05             <tr align="center">
06               <th align="center" bgcolor="#ffffff" width="5%">&nbsp;</th>
07               <th align="center" bgcolor="#ffffff" width="25%">{$lang.name}</th>
08               <th align="center" bgcolor="#ffffff">{$lang.describe}</th>
09               <th align="center" bgcolor="#ffffff" width="15%">{$lang.fee}</th>
10               <th align="center" bgcolor="#ffffff" width="15%">{$lang.free_money}</th>
11               <th align="center" bgcolor="#ffffff" width="15%">{$lang.insure_fee}</th>
12             </tr>
13             <!-- {foreach from=$shipping_list item=shipping} 循环配送方式 -->
14             <tr align="center">
15               <td align="center" bgcolor="#ffffff" valign="top"><input name="shipping" id="shipping_se" type="radio" value="{$shipping.shipping_id}" {if ($order.shipping_id eq $shipping.shipping_id) or true}checked="true"{/if} supportCod="{$shipping.support_cod}" insure="{$shipping.insure}" onclick="sel ectShipping(this)" />
16               </td>
17               <td align="center" bgcolor="#ffffff" valign="top"><strong>{$shipping.shipping_name}</strong></td>
18               <td align="center" bgcolor="#ffffff" valign="top">{$shipping.shipping_desc}</td>
19               <td bgcolor="#ffffff" align="center" valign="top">{$shipping.format_shipping_fee}</td>
20               <td bgcolor="#ffffff" align="center" valign="top">{$shipping.free_money}</td>
21               <td bgcolor="#ffffff" align="center" valign="top">{if $shipping.insure neq 0}{$shipping.insure_formated}{else}{$lang.not_support_insure}{/if}</td>
22             </tr>
23             <!-- {/foreach} 循环配送方式 -->
24             <!-- LONGHTML --><script>sel ectShipping02({$sel ect_shipping_id});</script> <!-- END -->
25             <tr align="center">
26               <td colspan="6" bgcolor="#ffffff" align="center"><label for="ECS_NEEDINSURE">
27                 <input name="need_insure" id="ECS_NEEDINSURE" type="checkbox"  onclick="sel ectInsure(this.checked)" value="1" {if $order.need_insure}checked="true"{/if} {if $insure_disabled}disabled="true"{/if}  />
28                 {$lang.need_insure} </label></td>
29             </tr>
30           </table>
31     </div>
32     <div class="blank"></div>
33         <!--{else}-->
34         <input name = "shipping" type="radio" value = "-1" checked="checked"  style="display:none"/>
35         <!--{/if}-->

  5、经过上面多处增加/修改,测试一下运行效果。

     广东  首重10KG 7元,续重0.7元/KG 

 



下一篇: 优化Ecshop后台订单列表 查看商品列表人 上一篇: 修复ECSHOP优惠价格失效的BUG
 用户评论(共 0 条评论)
  • 暂时还没有任何用户评论
用户名: 匿名用户 E-mail:
当前心情:
评论内容:
验证码: captcha
返回顶部 返回首页
 
QQ在线咨询
售前电话热线
#
售前QQ客服