上文中提到了創建模塊的步驟以及如何根據Magento的規則創建插件的目錄和修改配置文件,接下來我們創建模塊來設定運費.
以EMS為例,首先創建一個文件位於:
app/etc/code/local/Plima/Ship/Model/Shipping/Carrier
文件名為:Pems
需要注意的是:class類名的結構為目錄結構,須和目錄保持統一,否則會報錯.
此類繼承自Mage_Shipping_Model_Carrier_Abstract,引入Mage_Shipping_Model_Carrier_Interface工廠.
抽象類中有一些方法可以研究研究.
引入工廠是爲了保持對象的統一性.我們接下來會重載工廠的方法
1.
class
Plima_Ship_Model_Shipping_Carrier_Pems
extends
Mage_Shipping_Model_Carrier_Abstract
2.
implements
Mage_Shipping_Model_Carrier_Interface
3.
{
4.
}
需要override两个方法:
collectRates() 和 getAllowedMethods()
collectRates()用于计算运费的.注意$request和$this->getConfigData();
$request为接受checkout时的对象,print_r()可以获得更多信息,当然也可以到magento官方的站點去看doc
url:http://docs.magentocommerce.com
由於我使用了SOAP的方式查詢數據,故在這裡就沒有寫計算公式,從
1.
$request
->setDestCountry(Mage::getModel(
'directory/country'
)->load(
$request
->getDestCountryId())->getIso2Code());
可以得到國家,也就是客戶收貨地址的國家,再通過$request可以獲得購物車里的產品重量.
這樣把自己的計算公式加入到代碼里就不是難事了:
01.
public
function
collectRates(Mage_Shipping_Model_Rate_Request
$request
)
02.
{
03.
if
(!
$this
->getConfigFlag(
'active'
)) {
04.
return
false;
05.
}
06.
$freeBoxes
= 0;
07.
if
(
$request
->getAllItems()) {
08.
foreach
(
$request
->getAllItems()
as
$item
) {
09.
if
(
$item
->getProduct()->isVirtual() ||
$item
->getParentItem()) {
10.
continue
;
11.
}
12.
if
(
$item
->getHasChildren() &&
$item
->isShipSeparately()) {
13.
foreach
(
$item
->getChildren()
as
$child
) {
14.
if
(
$child
->getFreeShipping() && !
$child
->getProduct()->isVirtual()) {
15.
$freeBoxes
+=
$item
->getQty() *
$child
->getQty();
16.
}
17.
}
18.
}
elseif
(
$item
->getFreeShipping()) {
19.
$freeBoxes
+=
$item
->getQty();
20.
}
21.
}
22.
}
23.
$this
->setFreeBoxes(
$freeBoxes
);
24.
$result
= Mage::getModel(
'shipping/rate_result'
);
25.
$shippingPrice
= FALSE;
26.
$shippingPrice
=
$this
->getFinalPriceWithHandlingFee(
$shippingPrice
);
27.
if
(
$shippingPrice
!== false) {
28.
$method
= Mage::getModel(
'shipping/rate_result_method'
);
29.
$method
->setCarrier(
$this
->_code);
30.
$method
->setCarrierTitle(
$this
->getConfigData(
'title'
));
31.
$method
->setMethod(
$this
->_code);
32.
//$method->setMethodTitle($this->getConfigData('name'));
33.
$method
->setMethodTitle(
strtoupper
(
$this
->_code));
34.
if
(
$request
->getFreeShipping() === true ||
$request
->getPackageQty() ==
$this
->getFreeBoxes()) {
35.
$shippingPrice
=
'0.00'
;
36.
}
37.
//mivec modify start
38.
//type formula in here
39.
$request
->setDestCountry(Mage::getModel(
'directory/country'
)->load(
$request
->getDestCountryId())->getIso2Code());
40.
$params
[
'shipper'
] =
'ems'
;
41.
$params
[
'country'
] =
$request
->getDestCountry();
42.
//$params['country'] = 'United States';
43.
$params
[
'weight'
] =
$request
->getPackageWeight();
44.
$params
[
'tax'
] =
$this
->getConfigData(
'tax_fee'
);
45.
//$params['currency'] = '6.85';
46.
//print_r($mivec->client);
47.
$_price
=
new
stdclass;
48.
try {
49.
$client
=
new
SoapClient(
$this
->getConfigData(
'gateway_url'
));
50.
$_result
=
$client
->get(
$params
);
51.
$_result
= unserialize(
$_result
);
52.
$_price
->first =
$_result
[
'price'
][
'first'
];
53.
} catch (Exception
$e
) {
54.
//echo $e->getCode();
55.
}
56.
$_price
->first = !
empty
(
$_price
->first) ?
$_price
->first : 20;
57.
$shippingPrice
= !
empty
(
$shippingPrice
) ?
$shippingPrice
: 0;
58.
$method
->setPrice(
$shippingPrice
);
59.
$method
->setCost(
$shippingPrice
);
60.
$result
->append(
$method
);
61.
}
62.
return
$result
;
63.
}
allowedMethod()方法:
name這個key為管理頁面填寫的title,這個方法是爲了checkout/onepage中顯示模塊的名稱.
1.
public
function
getAllowedMethods()
2.
{
3.
return
array
(
'pems'
=>
$this
->getConfigData(
'name'
));
4.
}
以下是文件的全部源碼,注意屬性 _code的值
01.
class
Plima_Ship_Model_Shipping_Carrier_Pems
extends
Mage_Shipping_Model_Carrier_Abstract
02.
implements
Mage_Shipping_Model_Carrier_Interface {
03.
protected
$_code
=
'pems'
;
04.
protected
$_request
;
05.
/**
06.
* Enter description here...
07.
*
08.
* @param Mage_Shipping_Model_Rate_Request $data
09.
* @return Mage_Shipping_Model_Rate_Result
10.
*/
11.
public
function
collectRates(Mage_Shipping_Model_Rate_Request
$request
)
12.
{
13.
if
(!
$this
->getConfigFlag(
'active'
)) {
14.
return
false;
15.
}
16.
$freeBoxes
= 0;
17.
if
(
$request
->getAllItems()) {
18.
foreach
(
$request
->getAllItems()
as
$item
) {
19.
if
(
$item
->getProduct()->isVirtual() ||
$item
->getParentItem()) {
20.
continue
;
21.
}
22.
if
(
$item
->getHasChildren() &&
$item
->isShipSeparately()) {
23.
foreach
(
$item
->getChildren()
as
$child
) {
24.
if
(
$child
->getFreeShipping() && !
$child
->getProduct()->isVirtual()) {
25.
$freeBoxes
+=
$item
->getQty() *
$child
->getQty();
26.
}
27.
}
28.
}
elseif
(
$item
->getFreeShipping()) {
29.
$freeBoxes
+=
$item
->getQty();
30.
}
31.
}
32.
}
33.
$this
->setFreeBoxes(
$freeBoxes
);
34.
$result
= Mage::getModel(
'shipping/rate_result'
);
35.
$shippingPrice
= FALSE;
36.
$shippingPrice
=
$this
->getFinalPriceWithHandlingFee(
$shippingPrice
);
37.
if
(
$shippingPrice
!== false) {
38.
$method
= Mage::getModel(
'shipping/rate_result_method'
);
39.
$method
->setCarrier(
$this
->_code);
40.
$method
->setCarrierTitle(
$this
->getConfigData(
'title'
));
41.
$method
->setMethod(
$this
->_code);
42.
//$method->setMethodTitle($this->getConfigData('name'));
43.
$method
->setMethodTitle(
strtoupper
(
$this
->_code));
44.
if
(
$request
->getFreeShipping() === true ||
$request
->getPackageQty() ==
$this
->getFreeBoxes()) {
45.
$shippingPrice
=
'0.00'
;
46.
}
47.
//mivec modify start
48.
//type formula in here
49.
$request
->setDestCountry(Mage::getModel(
'directory/country'
)->load(
$request
->getDestCountryId())->getIso2Code());
50.
$params
[
'shipper'
] =
'ems'
;
51.
$params
[
'country'
] =
$request
->getDestCountry();
52.
//$params['country'] = 'United States';
53.
$params
[
'weight'
] =
$request
->getPackageWeight();
54.
$params
[
'tax'
] =
$this
->getConfigData(
'tax_fee'
);
55.
//$params['currency'] = '6.85';
56.
//print_r($mivec->client);
57.
$_price
=
new
stdclass;
58.
try {
59.
$client
=
new
SoapClient(
$this
->getConfigData(
'gateway_url'
));
60.
$_result
=
$client
->get(
$params
);
61.
$_result
= unserialize(
$_result
);
62.
$_price
->first =
$_result
[
'price'
][
'first'
];
63.
} catch (Exception
$e
) {
64.
//echo $e->getCode();
65.
}
66.
$_price
->first = !
empty
(
$_price
->first) ?
$_price
->first : 20;
67.
$shippingPrice
= !
empty
(
$shippingPrice
) ?
$shippingPrice
: 0;
68.
$method
->setPrice(
$shippingPrice
);
69.
$method
->setCost(
$shippingPrice
);
70.
$result
->append(
$method
);
71.
}
72.
return
$result
;
73.
}
74.
public
function
getAllowedMethods()
75.
{
76.
return
array
(
'pems'
=>
$this
->getConfigData(
'name'
));
77.
}
78.
}