您现在所在的位置:首页 > 圈子 > ECSHOP > 正文

Ecshop实现仿淘宝按地区显示运费终极完美版

2016-09-03 14:35:59    点击:

使用ECSHOP的同学都知道,ECSHOP在处理不同商品不同运费的时候非常尴尬
为此网上有很多方法,大都很雷同,而网上公开流行的版本“Ecshop实现仿淘宝按地区显示运费”会有很多BUG
因为我也用到,所以也研究了一下,总结笔记,方便后来者

使用程序:ECSHOP3.0(其他版本的同学们可以试试)

=======================华丽的分割线=======================
1.ECSHOP后台配送方式的创建
进入Ecshop后台”系统设置–>配送方式”,配置好您要使用到的配送方式,记住每一个配送ID(字段shipping_id)
特别提示:后台配送方式里面必须设置省份。


2、商品绑定配送方式的运费模板
2.1 数据表“ecs_goods”增加一个字段,执行下面SQL语句:
ALTER TABLE  `ecs_goods` ADD `shipping_id` MEDIUMINT(9) NOT NULL DEFAULT '2';

此代码的意思是在ecs_goods表里面添加一个shipping_id字段,默认值为2,作用是为每一个商品添加默认的指定的运费模板。
2.2 后台添加/编辑 商品 调出已经安装配送方式 “admin/ goods.php “,将此shipping_list函数添加到goods.php最末处。

/**
 * 取得已安装的配送方式
 * @return  array   已安装的配送方式
*/
function shipping_list()
{
    $sql = 'SELECT shipping_id, shipping_name ' .
            'FROM ' . $GLOBALS['ecs']->table('shipping') .
            ' WHERE enabled = 1';

    return $GLOBALS['db']->getAll($sql);
}

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

// LONGHTML 增加运费模板
$smarty->assign('shipping_list', shipping_list());
// END
$smarty->assign('unit_list', get_unit_list());

在“/* 处理商品数据 */”后面,增加POST过来的“shipping_id ”表单值进行赋值

/* 处理商品数据 */

// LONGHTML 运费模板(新增,更新)
$shipping_id = empty($_POST['shipping_id']) ? '0' : intval($_POST['shipping_id']);
// END

最后一步是“插入/更新”商品时,对“shipping_id”字段实现处理。网上让直接复制粘贴的代码有误,在这里我贴出正确的代码



共有5处地方要添加
 

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

<tr>
          <td class="label">运费模板</td>
          <td><select name="shipping_id" ><option value="0">{$lang.select_please}
		{foreach from=$shipping_list item=shipping}
                <option value="{$shipping.shipping_id}" {if $shipping.shipping_id eq $goods.shipping_id}selected{/if}>{$shipping.shipping_name}</option>
              {/foreach}
 </select>{$lang.require_field}</td>
        </tr>

在品牌下面,增加绑定运费模板。
 

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

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

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

/***** 商品页按地区显示运费 ***********************************************************************/
 $shippings = array();
 $res = $db->GetAll("SELECT shipping_name, shipping_id FROM ecs_shipping WHERE shipping_id=".$goods['shipping_id']);
 foreach ($res as $value)
 {
 $areas = array();
 $res1 = $db->GetAll("SELECT * FROM ecs_shipping_area WHERE shipping_id = $value[shipping_id]");
 foreach ($res1 as $area)
 {
 $configure = unserialize($area['configure']);
 if (is_array($configure))
 {
 foreach ($configure as $c)
 {
 if ($c['name'] == 'base_fee')
 {
 $price = $c['value'];
 }
 }
 }
 $sql = "SELECT a.region_id, r.region_name ".
 "FROM ".$ecs->table('area_region')." AS a, ".$ecs->table('region'). " AS r ".
 "WHERE r.region_id=a.region_id AND a.shipping_area_id='$area[shipping_area_id]'";
 $res2 = $db->query($sql);
 while ($arr = $db->fetchRow($res2))
 {
 $value['areas'][$arr['region_name']] = $price;
 }
 }
 $shippings[] = $value;
 }
 $res = $db->GetAll("SELECT region_id,region_name FROM ecs_region WHERE parent_id = 1");
 if($goods['shipping_id'] == 2)//默认配送模板的id

 {
 $current_region = '广东';   //默认显示广东省
 $smarty->assign('current_region',   $current_region); 
 $smarty->assign('current_price',   '7'); //默认显示的价格

 }

 foreach ($res as $value)
 {
 $row = array();
 foreach ($shippings as $a => $shipping)
 {
 if ($shipping['areas'])
 {
 foreach ($shipping['areas'] as $key => $price)
 {
 if ($key == $value['region_name'])
 {
 $row[$a]['shipping_price'] = $price;
 }
 }
 }
 if ($row[$a]['shipping_price'] > 0)
 {
 $row[$a]['shipping_name'] = $shipping['shipping_name'];
 $value['shippings'] = $row;
 }
 }
 if ($value['shippings']) $regions[] = $value;
 }
 $smarty->assign('regions',              $regions);
 /****************************************************************************/

goods.dwt  加在需要显示运费的地方,根据自己需要调整。

 <!--{if $regions}-->
 <script src="themes/default/js/chrome.js" type="text/javascript"></script>
 {foreach from=$regions key=key item=value}
 {if $key == 0}
 <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">
 {foreach from=$value.shippings item=shipping}
 {$shipping.shipping_name}{$current_price}元 &nbsp;
 {/foreach}
 </b>
 </p>
 {/if}
 {/foreach}
 <div id="dropmenu1" class="dropmenudiv">
 {foreach from=$regions item=value}
 <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>
 {/foreach}
 </div>
 <script>
 function show_shipping(name,price)
 {
 document.getElementById("s_a_name").innerHTML = name;
 document.getElementById("s_a_price").innerHTML = price;
 }
 cssdropdown.startchrome("chromemenu");
 </script>
 <style>
 #chromemenu b { font-weight:normal}
 .dropmenudiv {position:absolute;top: 0;z-index:100;width:200px;visibility: hidden; background:#fdffee; padding:8px; border:solid #ffbf69 2px; line-height:25px;}
 .dropmenudiv a { margin:0 5px 0 5px;}
 </style>
 <!--{/if}-->

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

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

/**
 * 获得上一次用户采用的支付和配送方式
 *
 * @access  public
 * @return  void
 */
function last_shipping_and_payment()
{
    $sql = "SELECT shipping_id, pay_id " .
            " FROM " . $GLOBALS['ecs']->table('order_info') .
            " WHERE user_id = '$_SESSION[user_id]' " .
            " ORDER BY order_id DESC LIMIT 1";
    $row = $GLOBALS['db']->getRow($sql);

	/* LONGHTML 获得购物车中商品 运费模板最大值 */
	$sql = "SELECT DISTINCT max(g.shipping_id) as  shipping_id " .
            " FROM " . $GLOBALS['ecs']->table('cart') ." AS c ".
            " LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g ON c.goods_id = g.goods_id" .
            " WHERE c.`session_id` =  '" . SESS_ID . "'".
			" AND c.`extension_code` !=  'package_buy' ";
    $shipping_id = $GLOBALS['db']->getOne($sql);
	$row['shipping_id'] = $shipping_id;
	// END

    if (empty($row))
    {
        /* 如果获得是一个空数组,则返回默认值 */
        $row = array('shipping_id' => 0, 'pay_id' => 0);
    }

    return $row;
}

4.2  flow.php购物流程checkout,done步骤,调用商品绑定的配送方式

   /* 对是否允许修改购物车赋值 */
   if ($flow_type != CART_GENERAL_GOODS || $_CFG['one_step_buy'] == '1')
   {
       $smarty->assign('allow_edit_cart', 0);
   }
   else
   {
       $smarty->assign('allow_edit_cart', 1);
   }

// LONGHTML 最大值的运费模板 
$arr = last_shipping_and_payment();
$_SESSION['flow_order']['shipping_id'] = $arr['shipping_id'];
$smarty->assign('select_shipping_id', $arr['shipping_id']);
// END

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

<!--{if $total.real_goods_count neq 0}-->
    <div class="" style="display:none;">
    <h5><span>{$lang.shipping_method}</span></h5>
    <table width="984" align="center" border="0" cellpadding="5" cellspacing="1" bgcolor="#dddddd" id="shippingTable">
            <tr align="center">
              <th align="center" bgcolor="#ffffff" width="5%">&nbsp;</th>
              <th align="center" bgcolor="#ffffff" width="25%">{$lang.name}</th>
              <th align="center" bgcolor="#ffffff">{$lang.describe}</th>
              <th align="center" bgcolor="#ffffff" width="15%">{$lang.fee}</th>
              <th align="center" bgcolor="#ffffff" width="15%">{$lang.free_money}</th>
              <th align="center" bgcolor="#ffffff" width="15%">{$lang.insure_fee}</th>
            </tr>
            <!-- {foreach from=$shipping_list item=shipping} 循环配送方式 -->
            <tr align="center">
              <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="selectShipping(this)" />
              </td>
              <td align="center" bgcolor="#ffffff" valign="top"><strong>{$shipping.shipping_name}</strong></td>
              <td align="center" bgcolor="#ffffff" valign="top">{$shipping.shipping_desc}</td>
              <td bgcolor="#ffffff" align="center" valign="top">{$shipping.format_shipping_fee}</td>
              <td bgcolor="#ffffff" align="center" valign="top">{$shipping.free_money}</td>
              <td bgcolor="#ffffff" align="center" valign="top">{if $shipping.insure neq 0}{$shipping.insure_formated}{else}{$lang.not_support_insure}{/if}</td>
            </tr>
            <!-- {/foreach} 循环配送方式 -->
			<!-- LONGHTML --><script>selectShipping02({$select_shipping_id});</script> <!-- END -->
            <tr align="center">
              <td colspan="6" bgcolor="#ffffff" align="center"><label for="ECS_NEEDINSURE">
                <input name="need_insure" id="ECS_NEEDINSURE" type="checkbox"  onclick="selectInsure(this.checked)" value="1" {if $order.need_insure}checked="true"{/if} {if $insure_disabled}disabled="true"{/if}  />
                {$lang.need_insure} </label></td>
            </tr>
          </table>
    </div>
    <div class="blank"></div>
        <!--{else}-->
        <input name = "shipping" type="radio" value = "-1" checked="checked"  style="display:none"/>
        <!--{/if}-->

感谢阿牛ECSHOP的资料来源

相关热词搜索:Ecshop 淘宝 运费 终极