Ecshop實現仿Taobao地區運費模板

目錄:javascript

一、Ecshop後臺配送方式建立
二、商品綁定配送方式的運費模板
2.1 數據表「ecs_goods」增長一個字段,執行下面SQL語句:
2.2 後臺添加/編輯 商品 調出已經安裝配送方式 "admin/ goods.php ",將此shipping_list函數添加到goods.php最末處。
2.3 後臺添加/編輯商品 實現綁定配送方式"admin/goods_info.htm"
三、前臺商品詳情調用設置好的配送方式
四、結算流程中,根據配送地址計算運費
4.1 重寫「include/lib_order.php」中last_shipping_and_payment函數。多個商品,不一樣配送方式,調用配送方式ID,以最貴配送方式計算。買家能夠找客服進行,運費改價。
五、通過上面多處增長/修改,測試一下運行效果。php

淘寶網(Taobao)購物的寶貝詳情頁面,能夠針對不一樣地區顯示不一樣運費,運費由後臺設定;結算時間,按重量、件數計算運費。Ecshop自己有配送方式插件,已有多家物流公司插件,例如:順豐快遞、申通快遞、圓通快遞等。本文介紹如何實現按地區顯示運費,而且讓每一個商品綁定運費模板。css

     一、Ecshop後臺配送方式建立

      進入Ecshop後臺"系統設置-->配送方式",將「順豐快遞」更名稱爲「糧食快遞」,配送ID號爲6。html

二、商品綁定配送方式的運費模板

       2.1 數據表「ecs_goods」增長一個字段,執行下面SQL語句:

ALTER TABLE  `ecs_goods` ADD `shipping_id` MEDIUMINT(9) NOT NULL DEFAULT '6';

  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());」增長調用代碼java

// LONGHTML 增長運費模板
$smarty->assign('shipping_list', shipping_list());
// END
$smarty->assign('unit_list', get_unit_list());

 在「/* 處理商品數據 */」後面,增長POST過來的「shipping_id 」表單值進行賦值 sql

/* 處理商品數據 */

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

最後一步是「插入/更新」商品時,對「shipping_id」字段實現處理。直接替換掉下面代碼chrome

/* 入庫 */
    if ($is_insert)
    {
        if ($code == '')
        {
            $sql = "INSERT INTO " . $ecs->table('goods') . " (goods_name, goods_name_style, goods_sn, " .
                    "cat_id, brand_id, shop_price, logi_cost, market_price, is_promote, promote_price, " .
                    "promote_start_date, promote_end_date, goods_img, index_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, province, city, virtual_buy,shipping_id)" .
                "VALUES ('$_POST[goods_name]', '$goods_name_style', '$goods_sn', '$catgory_id', " .
                    "'$brand_id', '$shop_price', '$logi_cost', '$market_price', '$is_promote','$promote_price', ".
                    "'$promote_start_date', '$promote_end_date', '$goods_img', '$index_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', '$goods_provincestr', '$goods_citystr', '$virtual_buy', '$shipping_id' )";
        }
        else
        {
            $sql = "INSERT INTO " . $ecs->table('goods') . " (goods_name, goods_name_style, goods_sn, " .
                    "cat_id, brand_id, shop_price, logi_cost, market_price, is_promote, promote_price, " .
                    "promote_start_date, promote_end_date, goods_img, index_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, province, city, virtual_buy,shipping_id)" .
                "VALUES ('$_POST[goods_name]', '$goods_name_style', '$goods_sn', '$catgory_id', " .
                    "'$brand_id', '$shop_price', '$logi_cost', '$market_price', '$is_promote','$promote_price', ".
                    "'$promote_start_date', '$promote_end_date', '$goods_img', '$index_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', '$goods_provincestr', '$goods_citystr', '$virtual_buy', '$shipping_id')";
        }
    }
    else
    {
        /* 若是有上傳圖片,刪除原來的商品圖 */
        $sql = "SELECT goods_thumb, goods_img, index_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']);
        }
        
        if ($index_img && $row['index_img'] && !goods_parse_url($row['index_img']))
        {
            @unlink(ROOT_PATH . $row['index_img']);
        }

        $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', " .
                "logi_cost = '$logi_cost', " .
                "market_price = '$market_price', " .
                "is_promote = '$is_promote', " .
                "promote_price = '$promote_price', " .
                "promote_start_date = '$promote_start_date', " .
                "suppliers_id = '$suppliers_id', " .
                "province = '$goods_provincestr', " .
                "city = '$goods_citystr', " .
                "virtual_buy = '$virtual_buy', " .
                "shipping_id = '$shipping_id', " .
                "promote_end_date = '$promote_end_date', ";

        /* 若是有上傳圖片,須要更新數據庫 */

2.3 後臺添加/編輯商品 實現綁定配送方式"admin/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>

在品牌下面,增長綁定運費模板。效果以下:數據庫

三、前臺商品詳情調用設置好的配送方式

      以主題default爲例,增長新文件:
          一、chrome.js (themes/default/js)
          二、icon_2.jpg (themes/default/images)數組

goods.php頁面商品顯示部分加入調用代碼session

/***** 商品頁按地區顯示運費 ***********************************************************************/
 $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'] == 6)
 {
 $current_region = '廣東';   //默認顯示廣東省
 $smarty->assign('current_region', &nbsp; $current_region); 
 $smarty->assign('current_price', &nbsp; '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', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$regions);
 /****************************************************************************/

goods.dwt  加在須要顯示運費的地方

&nbsp;<!--{if $regions}-->
 <script src="themes/yihaodian/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.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
   /* 檢查收貨人信息是否完整 */
   if (!check_consignee_info($consignee, $flow_type))
   {
       /* 若是不完整則轉向到收貨人信息填寫界面 */
       ecs_header("Location: flow.php?step=consignee\n");
       exit;
   }

   $_POST['how_oos'] = isset($_POST['how_oos']) ? intval($_POST['how_oos']) : 0;
   $_POST['card_message'] = isset($_POST['card_message']) ? htmlspecialchars($_POST['card_message']) : '';
   $_POST['inv_type'] = !empty($_POST['inv_type']) ? htmlspecialchars($_POST['inv_type']) : '';
   $_POST['inv_payee'] = isset($_POST['inv_payee']) ? htmlspecialchars($_POST['inv_payee']) : '';
   $_POST['inv_content'] = isset($_POST['inv_content']) ? htmlspecialchars($_POST['inv_content']) : '';
   $_POST['postscript'] = isset($_POST['postscript']) ? htmlspecialchars($_POST['postscript']) : '';

// LONGHTML 最大值的運費模板 
$arr = last_shipping_and_payment();
$_SESSION['flow_order']['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}-->

 五、通過上面多處增長/修改,測試一下運行效果。

廣東  首重10KG 7元,續重0.7元/KG 

 PS:感謝我的站長重慶紋繡的幫助。

相關文章
相關標籤/搜索