magneto创建运费模板

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上测试通过。

上一篇:入门级的按键驱动——按键驱动笔记之poll机制-异步通知-同步互斥阻塞-定时器防抖


下一篇:Linux内核分析_课程学习总结报告