[转]Magento 自定义EMS FEDEX DHL UPS扩展 第二章

4,032次阅读
没有评论

共计 6068 个字符,预计需要花费 16 分钟才能阅读完成。

上文中提到了創建模塊的步驟以及如何根據Magento的規則創建插件的目錄和修改配置文件,接下來我們創建模塊來設定運費.

以EMS為例,首先創建一個文件位於:
app/etc/code/local/Plima/Ship/Model/Shipping/Carrier

文件名為:Pems
需要注意的是:class類名的結構為目錄結構,須和目錄保持統一,否則會報錯.
此類繼承自Mage_Shipping_Model_Carrier_Abstract,引入Mage_Shipping_Model_Carrier_Interface工廠.
抽象類中有一些方法可以研究研究.
引入工廠是爲了保持對象的統一性.我們接下來會重載工廠的方法
[php]
<div id="&quot;highlighter_">

lass Plima_Ship_Model_Shipping_Carrier_Pems extendsMage_Shipping_Model_Carrier_Abstract
mplements Mage_Shipping_Model_Carrier_Interface
[/php]

需要override两个方法:
collectRates() 和 getAllowedMethods()
collectRates()用于计算运费的.注意$request和$this->getConfigData();
$request为接受checkout时的对象,print_r()可以获得更多信息,当然也可以到magento官方的站點去看doc
url:http://docs.magentocommerce.com

由於我使用了SOAP的方式查詢數據,故在這裡就沒有寫計算公式,從
[php]

request-&gt;setDestCountry(Mage::getModel(‘directory/country’)-&gt;load($request-&gt;getDestCountryId())-&gt;getIsode());
[/php]
可以得到國家,也就是客戶收貨地址的國家,再通過$request可以獲得購物車里的產品重量.
這樣把自己的計算公式加入到代碼里就不是難事了:
[php]

public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
if (!$this-&gt;getConfigFlag(‘active’)) {
return false;
}
$freeBoxes = 0;
if ($request-&gt;getAllItems()) {
foreach ($request-&gt;getAllItems() as $item) {
if ($item-&gt;getProduct()-&gt;isVirtual() || $item-&gt;getParentItem()) {
continue;
}
($item-&gt;getHasChildren() &amp;&amp; $item-&gt;isShipSeparately()) {
each ($item-&gt;getChildren() as $child) {
($child-&gt;getFreeShipping() &amp;&amp; !$child-&gt;getProduct()-&gt;isVirtual()) {
eeBoxes += $item-&gt;getQty() * $child-&gt;getQty();

} elseif ($item-&gt;getFreeShipping()) {
$freeBoxes += $item-&gt;getQty();
}
}
}
is-&gt;setFreeBoxes($freeBoxes);
sult = Mage::getModel(‘shipping/rate_result’);
ippingPrice = FALSE;
ippingPrice = $this-&gt;getFinalPriceWithHandlingFee($shippingPrice);
($shippingPrice !== false) {
$method = Mage::getModel(‘shipping/rate_result_method’);
$method-&gt;setCarrier($this-&gt;_code);
$method-&gt;setCarrierTitle($this-&gt;getConfigData(‘title’));
$method-&gt;setMethod($this-&gt;_code);
//$method-&gt;setMethodTitle($this-&gt;getConfigData(‘name’));
$method-&gt;setMethodTitle(strtoupper($this-&gt;_code));
($request-&gt;getFreeShipping() === true || $request-&gt;getPackageQty() ==$this-&gt;getFreeBoxes()) {
ippingPrice = ‘0.00’;

ivec modify start
//type formula in here
$request-&gt;setDestCountry(Mage::getModel(‘directory/country’)-&gt;load($request-&gt;getDestCountryId())-&gt;getIsode());
$params[‘shipper’] = ’ems’;
$params[‘country’] = $request-&gt;getDestCountry();
//$params[‘country’] = ‘United States’;
$params[‘weight’] = $request-&gt;getPackageWeight();
$params[‘tax’] = $this-&gt;getConfigData(‘tax_fee’);
params[‘currency’] = ‘
rint_r($mivec-&gt;client);
rice = new stdclass;
try {
$client = new SoapClient($this-&gt;getConfigData(‘gateway_url’));
$_result = $client-&gt;get($params);
$_result = unserialize($_result);
$_price-&gt;first = $_result[‘price’][‘first’];
} catch (Exception $e) {
//echo $e-&gt;getCode();
}
rice-&gt;first = !empty($_price-&gt;first) ? $_price-&gt;first :
ippingPrice = !empty($shippingPrice) ? $shippingPrice : 0;
$method-&gt;setPrice($shippingPrice);
$method-&gt;setCost($shippingPrice);
$result-&gt;append($method);
}
return $result;
}
[/php]
allowedMethod()方法:
name這個key為管理頁面填寫的title,這個方法是爲了checkout/onepage中顯示模塊的名稱.
[php]

ublic function getAllowedMethods()

eturn array(‘pems’=&gt;$this-&gt;getConfigData(‘name’));
[/php]
以下是文件的全部源碼,注意屬性 _code的值
[php]

<a title="view source" href="http://mivec.ixsbn.com/archives/create-ship-module-for-magento-ems-dhl-fedex-ups-part-ii#viewSource">view source</a> <a title="print" href="http://mivec.ixsbn.com/archives/create-ship-module-for-magento-ems-dhl-fedex-ups-part-ii#printSource">print</a><a title="?" href="http://mivec.ixsbn.com/archives/create-ship-module-for-magento-ems-dhl-fedex-ups-part-ii#about">?</a>

class Plima_Ship_Model_Shipping_Carrier_Pems extendsMage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface {
protected $_code = ‘pems’;
protected $_request;
/**
* Enter description here…
*
* @param Mage_Shipping_Model_Rate_Request $data
* @return Mage_Shipping_Model_Rate_Result
*/
public function collectRates(Mage_Shipping_Model_Rate_Request $request)

(!$this-&gt;getConfigFlag(‘active’)) {
urn false;

eeBoxes = 0;
($request-&gt;getAllItems()) {
foreach ($request-&gt;getAllItems() as $item) {
if ($item-&gt;getProduct()-&gt;isVirtual() || $item-&gt;getParentItem()) {
continue;
}
if ($item-&gt;getHasChildren() &amp;&amp; $item-&gt;isShipSeparately()) {
each ($item-&gt;getChildren() as $child) {
($child-&gt;getFreeShipping() &amp;&amp; !$child-&gt;getProduct()-&gt;isVirtual()) {
eeBoxes += $item-&gt;getQty() * $child-&gt;getQty();

} elseif ($item-&gt;getFreeShipping()) {
$freeBoxes += $item-&gt;getQty();
}
}
}
$this-&gt;setFreeBoxes($freeBoxes);
sult = Mage::getModel(‘shipping/rate_result’);
ippingPrice = FALSE;
ippingPrice = $this-&gt;getFinalPriceWithHandlingFee($shippingPrice);
($shippingPrice !== false) {
$method = Mage::getModel(‘shipping/rate_result_method’);
$method-&gt;setCarrier($this-&gt;_code);
$method-&gt;setCarrierTitle($this-&gt;getConfigData(‘title’));
$method-&gt;setMethod($this-&gt;_code);
//$method-&gt;setMethodTitle($this-&gt;getConfigData(‘name’));
$method-&gt;setMethodTitle(strtoupper($this-&gt;_code));
if ($request-&gt;getFreeShipping() === true || $request-&gt;getPackageQty() == $this-&gt;getFreeBoxes()) {
ippingPrice = ‘0.00’;

ivec modify start
//type formula in here
$request-&gt;setDestCountry(Mage::getModel(‘directory/country’)-&gt;load($request-&gt;getDestCountryId())-&gt;getIsode());
$params[‘shipper’] = ’ems’;
$params[‘country’] = $request-&gt;getDestCountry();
//$params[‘country’] = ‘United States’;
$params[‘weight’] = $request-&gt;getPackageWeight();
$params[‘tax’] = $this-&gt;getConfigData(‘tax_fee’);
//$params[‘currency’] = ‘
rint_r($mivec-&gt;client);
rice = new stdclass;
try {
$client = new SoapClient($this-&gt;getConfigData(‘gateway_url’));
$_result = $client-&gt;get($params);
$_result = unserialize($_result);
$_price-&gt;first = $_result[‘price’][‘first’];
} catch (Exception $e) {
//echo $e-&gt;getCode();
}
$_price-&gt;first = !empty($_price-&gt;first) ? $_price-&gt;first :
ippingPrice = !empty($shippingPrice) ? $shippingPrice : 0;
$method-&gt;setPrice($shippingPrice);
$method-&gt;setCost($shippingPrice);
$result-&gt;append($method);
}
return $result;
}
public function getAllowedMethods()
{
return array(‘pems’=&gt;$this-&gt;getConfigData(‘name’));
}
}
[/php]

正文完
 0
评论(没有评论)

空瓶子部落

文章搜索
推荐阅读
提高过程能力指数(CP/CPK)的途径

提高过程能力指数(CP/CPK)的途径

编者按:过程能力指数(CP/CPK)想必各位质量人都耳熟能详、运用自如,质量工程师之家前期也共享过数篇关于过程...
销售管理者如何驱动团队拿业绩?

销售管理者如何驱动团队拿业绩?

这些年,我接触过太多销售团队。有的团队能持续突破业绩目标,有的团队却始终在原地打转。为什么同样的产品、同样的市...
质量检验——如何设置质量检验站

质量检验——如何设置质量检验站

质量检验的进行离不开检验站,而如何设置合理的检验站是一项科学细致的组织工作,也是一个经济性的问题。合理设置检验...
什么是质量

什么是质量

一、质量的定义   质量是一个多义词,在不同的领域有着不同的含义,工程质量通常指产品或工程的优劣程度。   1...
最新文章
群晖 Let’s Encrypt 泛域名证书自动更新

群晖 Let’s Encrypt 泛域名证书自动更新

目前acme协议版本更新,开始支持泛域名(wildcard),也就是说,可以申请一个类似*.domain.co...
可以卸载TV Box 了,这款支持「绅士模式」的影视神器你值得拥有

可以卸载TV Box 了,这款支持「绅士模式」的影视神器你值得拥有

还在为找优秀片源难、广告多、平台会员太贵而烦恼?今天给大家挖到一款真正的影视宝藏工具——小猫影视! 作为开源免...
【收藏】一次性解决TV点播/直播自由

【收藏】一次性解决TV点播/直播自由

很多时候,资源就在面前,但是我们视而不见,因为长久的安逸,已经让人失去动手的兴趣。但是每次我需要挨个切换APP...
OpenWrt 存储空间扩容的两种方案

OpenWrt 存储空间扩容的两种方案

说明:当我们通过群晖 VMM 虚拟机安装 Open­Wrt 时,默认会分配一个 10GB 的存储空间,而实际情...
OpenWrt修改IP地址两种方法(直接命令修改跟后台修改)

OpenWrt修改IP地址两种方法(直接命令修改跟后台修改)

OpenWrt是什么?OpenWrt一般常见于无线路由器(软路由)第三方固件,它是一个高效、可靠、功能多的路由...
热门文章
提高过程能力指数(CP/CPK)的途径

提高过程能力指数(CP/CPK)的途径

编者按:过程能力指数(CP/CPK)想必各位质量人都耳熟能详、运用自如,质量工程师之家前期也共享过数篇关于过程...
SPC控制图的八种模式分析

SPC控制图的八种模式分析

SPC控制图有八种模式,即八种判断异常的检验准则,每一种检验准则代表一种异常现象,应用SPC控制图进行过程评估...
测量高手放大招:圆跳动测量技巧总结

测量高手放大招:圆跳动测量技巧总结

01. 前言 在五金机加工厂实际的测量工作中,经常碰到要求测量两个要素的圆跳动问题, 利用不同的测量辅件及夹具...
过程能力分析(CP&cpk)

过程能力分析(CP&cpk)

引入过程能力分析的目的? 在我们现有的管理过程中,我们经常会遇到有些具体指标总是不尽人意,存在许多需要改进的地...
新能源汽车 “两会”精神宣贯会

新能源汽车 “两会”精神宣贯会

此次和大家分享新能源汽车相关政策: [embeddoc url=”https://www.ctro...
最新评论
多乐士 多乐士 通过摸索发现ssh拉取会报错,直接网页访问下载会报404错误,不知道原因;但是可以通过群晖CM注册表访问下载,其方法如下: Container Manager-注册表-设置-新增-注册表名称随便写,注册表URL填你的加速地址,勾选信任的SSL自我签署证书,登录信息不填-应用-使用你的地址,这是注册表会显示了,在搜索栏中输入映像名称,搜索结果在每一页的最后一个,你需要划到最后一个进行下载,实测可正常下载安装。 以上供网友参考。
多乐士 多乐士 还有一个比较简单的方法,只是需要一些外部工具。 1、讲损毁硬盘取出,装入外部移动硬盘 2、打开Diskgenius,定位到硬盘 3、格式化系统分区 4、重新插入硬盘 5、存储池->修复存储池即可
多乐士 多乐士 写的不错的文章
辞了老衲 辞了老衲 这个确实有帮助。
渋驀 渋驀 当然任何时候都可以用curl命令和crontab来实现动态更新DDNS的ip地址: 1、安装crontab之后为root用户创建文件/var/spool/cron/root 2、创建并配置ddnsupdate.sh,放到/usr/bin/文件下,文件内容(以he.net为例): Autodetect my IPv4/IPv6 address: IPV4:curl -4 "http://dyn.example.com:password@dyn.dns.he.net/nic/update?hostname=dyn.example.com" IPV6:curl -6 "http://dyn.example.com:password@dyn.dns.he.net/nic/update?hostname=dyn.example.com" 3、添加执行权限chomod +x /usr/bin/ddnsupdate.sh 4、编辑root用户的crontab:*/10 * * * * /usr/binddnsupdate.sh,每10分钟执行一次。好了,可以享受你的DDNS了
21410 21410 请问下载链接在那里?
madkylin madkylin 不错,不错,谢谢分享了,好东西啊 :lol:
feilung feilung 求方法
zengsuyi zengsuyi 应该挺不错的
zise zise 看看是怎么操作的。。 :oops: