定制Magento货运模块

言:希望定制一个Magento货运模块,根据地址类型来选择不同的订单固定价格。具体来说,地址类型分为空运、海运和普通快递,价格分别为$130, $100, $120. 该模块使用如下命名约定:
公司名: Maxland
模块名: TrustedSC

这里假设你已经生成了模块的一般骨架结构,包括目录结构、config.xml等,并且已经激活了该模块。接下来描述货运模块所特有的东西

1.创建Shipping Method类
app/code/local/Maxland/TrustedSC/Model/Carrier/TrustedSC.php
<?php
class Maxland_TrustedSC_Model_Carrier_TrustedSC extends Mage_Shipping_Model_Carrier_Abstract{
	protected $_code = 'trustedsc';

	/**
	* 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){
		// skip if not enabled
		if (!Mage::getStoreConfig('carriers/'.$this->_code.'/active')) {
		  return false;
		}
		$carrier_prefix  = 'carriers/'.$this->_code;
		$result = Mage::getModel('shipping/rate_result');

		// 一个shipping module可以设置多种shipping method,这里只假有一种method.
		$method = Mage::getModel('shipping/rate_result_method');
		$method->setCarrier($this->_code);
		$method->setCarrierTitle(Mage::getStoreConfig($carrier_prefix.'/title'));
		$method->setMethod($this->_code . '_' . $this->_code);
		$method->setMethodTitle(Mage::helper('trustedsc')->__($quoteAddress->getType()));
		$method->setMethodDescription(Mage::getStoreConfig($carrier_prefix.'/description'));

		// 获得地址信息,需要为地址定制一个type字段,用来存放货运类型(这一步请自行完成)
		$quoteAddress = null;
		if ($request->getAllItems()) {
			foreach ($request->getAllItems() as $item) {
				$quoteAddress = $item->getAddress();
			}
		}
		if($quoteAddress->getType() == 'OCEAN_SHIPPING' ) {
			$method->setPrice(100);
		} else if($quoteAddress->getType() == 'AIR-EXPRESS' ) {
			$method->setPrice(130);
		} else if($quoteAddress->getType() == 'EXPRESS-DELIVERY' ) {
			$method->setPrice(120);
		}
		// add this rate to the result
		$result->append($method);

		return $result;
	}
}


说明:为了简化货运模块的概念,可以完全不必考虑上面代码中的type之类的,简单地设置一个固定价格$method->setPrice(100), 继续下面的操作。
collectRates是一个回调函数,入口参数$request可以获得本次购买的所有信息,包括购买的哪些产品,货运地址等。
Mage::getStoreConfig($carrier_prefix.'/title');则是读取system.xml文件中carrier/trusted/title标签的值,该值可以在后台配置更改(稍后介绍)

2.配置文件管理
这一步,需要告诉Magento在后台配置中显示货运配置,创建文件:
app/code/local/Maxland/TrustedSC/etc/system.xml
<?xml version="1.0"?>
<config>
  <sections>
    <carriers>
      <groups>
        <trustedsc translate="label" module="shipping">
			<label>TrustedSC</label>
			<frontend_type>text</frontend_type>
			<sort_order>13</sort_order>
			<show_in_default>1</show_in_default>
			<show_in_website>1</show_in_website>
			<show_in_store>1</show_in_store>
			<model>trustedsc/carrier_trustedsc</model>
			<fields>
				  <active translate="label">
					  <label>Enabled</label>
					  <frontend_type>select</frontend_type>
					  <source_model>adminhtml/system_config_source_yesno</source_model>
					  <sort_order>1</sort_order>
					  <show_in_default>1</show_in_default>
					  <show_in_website>1</show_in_website>
					  <show_in_store>1</show_in_store>
				  </active>
				  <title translate="label">
					  <label>Title</label>
					  <frontend_type>text</frontend_type>
					  <sort_order>2</sort_order>
					  <show_in_default>1</show_in_default>
					  <show_in_website>1</show_in_website>
					  <show_in_store>1</show_in_store>
				  </title>
				  <description translate="label">
					  <label>Carrier Desciption</label>
					  <frontend_type>textarea</frontend_type>
					  <sort_order>76</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>
        </trustedsc>
      </groups>
    </carriers>
  </sections>
</config>

显然,fields标签下就是后台控制面板中显示的字段,可以配置任意多个标签。然后给各个字段设置初始值,修改system.xml同目录下的config.xml,增加如下行:
<default>
 <carriers>
  <trustedsc>
   <active>1</active>
   <model>trustedsc/carrier_trustedsc</model>
   <name>TrustedSC</name>
   <title>TrustedSC</title>
   <description>The PROVIDER,SHIPPING FEE and PERIOD will be confirmed by us later. We'll inform you via email once we confirm it.</description>
  </trustedsc>
 </carriers>
</default>

注:<model>标签hook了前面的shipping method类。

3.观察效果
刷新后台页面看到:
定制Magento货运模块

前端下订单在shipping method选择页面看到:
定制Magento货运模块

以上仅仅是货运模块基本定制。更复杂定制可能包括:
1. 多个shipping method;
2. 前端checkout货运方法选择时的复杂用户交互(如点击货运方法的radiobutton弹出具体货运信息或要求用户输入等).

希望大家共享定制的研究成果。



你可能感兴趣的:(xml,PHP,配置管理,AIR)