Magento系统自带了大概7种运费方式:平价、运费表、免运费、ups、usps、fedex、dhl等。不过这些依然无法满足我们的需求,这时候就需要创建一个shipping module 来实现了。创建一个shipping module 很简单,需要继承Mage_Shipping_Model_Carrier_Abstract抽象类, 实现Mage_Shipping_Model_Carrier_Interface接口类,这样就能利用函数collectRates来自定义计算运费的方式。这样就可以创建一个插件来自定义shipping method。

添加模块配置信息

首先,添加模块信息,创建文件app/etc/modules/Xbc_Ship.xml

<?xml version="1.0"?>
<config>
<modules>
<Xbc_Ship>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Shipping />
</depends>
<version>1.1.0</version>
</Xbc_Ship>
</modules>
</config>

添加模块配置信息,创建文件app/code/local/Xbc/Ship/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
<Xbc_Ship>
<version>1.1.0</version>
</Xbc_Ship>
</modules>
<global>
<helpers>
<ship>
<class>Xbc_Ship_Helper</class>
</ship>
</helpers>
<resources>
<ship_setup>
<setup>
<module>Xbc_Ship</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</ship_setup>
<ship_write>
<connection>
<use>core_write</use>
</connection>
</ship_write>
<ship_read>
<connection>
<use>core_read</use>
</connection>
</ship_read>
</resources>
<models>
<ship>
<class>Xbc_Ship_Model</class>
<resourceModel>ship_mysql4</resourceModel>
</ship>
</models>
</global>
<default>
<carriers>
<cm_dhl>
<active>1</active>
<debug>0</debug>
<model>ship/carrier_cm_dhl</model>
<name>DHL</name>
<title>DHL</title>
<description>DHL</description>
<sort_order>0</sort_order>
</cm_dhl>
</carriers>
</default>
</config>

实现自定义运费

创建文件app/code/local/Hofan/Ship/Model/Carrier/Cm/Dhl.php。

<?php
class Xbc_Ship_Model_Carrier_Cm_Dhl
extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{
protected $_code = 'cm_dhl';
 
/**
* Collect rates for this shipping method based on information in $request
*
* @param Mage_Shipping_Model_Rate_Request $data
* @return Mage_Shipping_Model_Rate_Result
*/
public function collectRates(Mage_Shipping_Model_Rate_Request $request){
//if this shipping method disabled
if (!$this->getConfigFlag('active')) {
return false;
}
 
$result = Mage::getModel('shipping/rate_result');
$method = Mage::getModel('shipping/rate_result_method');
$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));
$method->setMethod($this->_code);
$method->setMethodTitle($this->getConfigData('name'));
 
$debug = $this->getConfigData('debug');
$rate = $this->getConfigData('rate');
 
//get find the country id
$country_id = $request->getDestCountryId();
 
//Get all items
$items = $request->getAllItems();
$weight = $request->getPackageWeight();
foreach ($items as $item){
$_product = $item->getProduct();
if ($_product instanceof Mage_Catalog_Model_Product) {
$product = Mage::getModel('catalog/product')->load($_product->getId());
if($_weight = $product->getWeight()){
 
}
}
}
 
//get price
$shippingPrice = 100;
 
$method->setPrice($shippingPrice);
$method->setCost($shippingPrice);
$result->append($method);
return $result;
}
 
/**
* Get allowed shipping methods
*
* @return array
*/
public function getAllowedMethods()
{
return array($this->_code=>$this->getConfigData('name'));
}
}

添加后台配置

如果完成了上面的步骤,你可以添加后台配置文件。创建文件app/code/local/Xbc/Ship/etc/system.xml

<?xml version="1.0"?>
<config>
<sections>
<carriers translate="label" module="ship">
<groups>
<cm_dhl translate="label">
<label>Hofan DHL</label>
<frontend_type>text</frontend_type>
<sort_order>0</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<model>ship/carrier_cm_dhl</model>
<fields>
<active translate="label">
<label>Enabled</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</active>
<debug translate="label">
<label>Debug Mode</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</debug>
<title translate="label">
<label>Title</label>
<frontend_type>text</frontend_type>
<sort_order>30</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</title>
<name translate="label">
<label>Method Name</label>
<frontend_type>text</frontend_type>
<sort_order>40</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</name>
<description translate="label">
<label>Description</label>
<frontend_type>textarea</frontend_type>
<sort_order>50</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</description>
<sort_order translate="label">
<label>Sort Order</label>
<frontend_type>text</frontend_type>
<sort_order>100</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</sort_order>
</fields>
</cm_dhl>
</groups>
</carriers>
</sections>
</config>

该插件在Magento CE 1.6.2上测试通过。

magneto创建运费模板的更多相关文章

  1. Ecshop实现仿Taobao地区运费模板

    目录: 1.Ecshop后台配送方式创建 2.商品绑定配送方式的运费模板 2.1 数据表“ecs_goods”增加一个字段,执行下面SQL语句: 2.2 后台添加/编辑 商品 调出已经安装配送方式 & ...

  2. 整理 PHPstorm实用个人配置,修改调整个性化快捷键,修改使用phpstorm创建的模板的默认注释:

    对你有助请点赞,请顶------送人玫瑰,手留余香! 1:58 2016/3/12 整理PHPstorm实用个人配置,修改调整个性化快捷键,修改使用phpstorm创建的模板的默认注释: PHPsto ...

  3. vs创建项目模板和项模板

    原文地址:https://msdn.microsoft.com/zhcn/library/xkh1wxd8(v=vs.140).aspx 如何:创建项目模板 Visual Studio 2015   ...

  4. zabbix通过API创建交换机模板,ifAdminStatus;ifOperStatus;ifInUcastPkts;ifAlias

    最终效果: 目的:         通过zabbix的Latest data查看主机就可以看到其监控结果. 监控项:         # 管理状态          IF-MIB::ifAdminSt ...

  5. VS2010/MFC编程入门之六(对话框:创建对话框模板和修改对话框属性)

    鸡啄米在上一讲中介绍了MFC的消息映射机制,属于原理方面的知识.对于VC++编程入门学习者来说可能有些抽象,鸡啄米会把消息映射的知识渗透到后面的教程中.本节开始为大家讲解偏应用的知识-创建对话框. 对 ...

  6. 【WPF】创建基于模板的WPF控件(经典)

    原文:[WPF]创建基于模板的WPF控件(经典) WPF可以创建两种控件,它们的名字也很容易让人混淆:用户控件(User Control)和定制控件(Customer Control),之所以如此命名 ...

  7. Sublime text3 创建html模板

    最近接手了公司官网跟新的任务,需要编写HTML页面.页面中存在大量重复内容(导航条.页脚.侧边栏等),每次复制粘贴也不是个事,网上搜了相关的HTML模板创建问题,还找到了.楼主使用的是Sublime ...

  8. 创建JDBC模板简化代码、JDBC应用的事务管理以及连接池的作用

    一.创建JDBC模板简化代码 一个简单的查询.要做这么一大堆事情,并且还要处理异常,我们不防来梳理一下: 1.获取connection  2.获取statement  3.获取resultset  4 ...

  9. 从零开始实现ASP.NET Core MVC的插件式开发(二) - 如何创建项目模板

    标题:从零开始实现ASP.NET Core MVC的插件式开发(二) - 如何创建项目模板 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/11155 ...

随机推荐

  1. MD4C/CO46/MD04一个很棒的工单缺料分析

    大家好~~~ 之前在MD04物料分析的时候,看到有订单预留,双击有个订单报告可以显示一个订单物料是否缺料清单 这个单独的工单分析可以在T-code:MD4C,CO46查看,如果只是单独的使用,那么这两 ...

  2. MySQL 建库、建用户及建表事项

    1,MySQL建库语句比较简单,一句话: create database tppamltest3 2,创建用户及授权: insert into mysql.user(Host,User,Passwor ...

  3. Ubuntu 下Eclipse 安装SVN

    如果尚未安装Eclipse,先安装:也可以直接下载Google提供的ADT Bundle. sudo apt-get install eclipse 安装Subversion sudo apt-get ...

  4. js 日期时间控制器

    /////////////////////////调用实例 // <div> // <span>交易查询:</span> <span>从 // < ...

  5. 部署步骤“回收 IIS 应用程序池”中出现错误: <nativehr>0x80070005</nativehr><nativestack></nativestack>拒绝访问。

    解决方法:以sharepoint管理员身份进入主站点,修改站点的网站集管理员.

  6. DataGridView批量执行Insert和Remove行时特别慢的解决方案

    向DataGridView循环插入110条数据耗时5秒多. 在循环前执行: var oldAutoSizeRowsMode = this.AutoSizeRowsMode; var oldAutoSi ...

  7. mark 一下

    Android资源管理框架(Asset Manager)简要介绍和学习计划 http://www.cnblogs.com/hjtdlx/p/4332060.html

  8. C#winform省市县联动,以及有的县是空值时显示异常的处理

    一.如下comboBox1.comboBox2.comboBox3,原来这三个都是空的, 将数据库中的省份传递到comboBox1中 我的数据库有parent字段,根据市的parent找到省,根据县的 ...

  9. 织梦dedecms分类信息模型上一页下一页失效办法

    修改文件/include/arc.archives.class 将一下代码 $next = (is_array($nextR) ? " where arc.id={$nextR['id']} ...

  10. Deep Learning In NLP 神经网络与词向量

    0. 词向量是什么 自然语言理解的问题要转化为机器学习的问题,第一步肯定是要找一种方法把这些符号数学化. NLP 中最直观,也是到目前为止最常用的词表示方法是 One-hot Representati ...