A-A+

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

2012年08月25日 灵犀一指 [转]Magento 自定义EMS FEDEX DHL UPS扩展 第二章已关闭评论 阅读 2,310 人 次

上文中提到了創建模塊的步驟以及如何根據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]

标签:

评论已关闭!