magento的运费模块开发教程(Customize Magento Shipping Module Tutorial)

好久好久没写文章了 已经懒了,之前翻译过一篇关于Magento新闻模块的开发教程,今天再写一篇Shipping Module的模块开发教程吧,示例出自Jamie Huskisson的《Magento 1.3: PHP Developer's Guide》,我按自己的意识表达 就不做翻译了,如果错误之处,欢迎指正吐槽!


1.搭一个模块 肯定要先告诉magento模块的配置文件吧,所以在app\etc\modules这里写一个模块文件告诉Magento我们要新建一个模块。我这里叫Yip_CustomShipping.xml,它是一个XML。

<config>
	<modules>
		<Yip_CustomShipping>
			<active>true</active>
			<codePool>local</codePool>
		</Yip_CustomShipping>
	</modules>
</config>

从代码可以看出 模块的命名空间是Yip 模块名称是CustomShipping 激活状态,模块路径位于local代码池,不好意思code pool我就这样翻译了。


2.告诉好Magento我们要写一个模块之后 我们就写模块具体的配置文件吧,到app\code\local\Yip\CustomShipping\etc这里新建一个config.xml 这个是模块的配置文件

<config>
	<modules>
		<Yip_CustomShipping>
			<version>0.1.0</version>
			<depends>
				<Mage_Shipping />
			</depends>
		</Yip_CustomShipping>
	</modules>
	<global>
		<models>
			<customshipping>
				<class>Yip_CustomShipping_Model</class>
			</customshipping>
		</models>
		<resources>
			<customshipping_setup>
				<setup>
					<module>Yip_CustomShipping</module>
				</setup>
				<connection>
					<use>core_setup</use>
				</connection>
			</customshipping_setup>
		</resources>
	</global>
	<default>
		<carriers>
			<customshipping>
				<model>customshipping/carrier_fullshipping</model>
			</customshipping>
		</carriers>
	</default>
</config>
这段代码首先有模块的版本,基于Magento内核的Shipping模块 然后指定模块的一般模型 最后是carriers类 我也不知道怎么翻译才好 暂时翻译成载体类吧 里面是Yip_CustomShipping_Model_Carrier_FullShipping这个具体的类 指定的路径为app\code\local\Yip\CustomShipping\Model\Carrier\FullShipping.php  于是我们就跑去新建一个载体类吧。


3.这个载体类的作用是extends内核类Mage_Shipping_Model_Carrier_Abstract 有的写法还做Mage_Shipping_Model_Carrier_Interface的接口 这里暂不提及。

<?php
class Yip_CustomShipping_Model_Carrier_FullShipping extends Mage_Shipping_Model_Carrier_Abstract
{
	protected $_code = 'customshipping';
	public function collectRates(Mage_Shipping_Model_Rate_Request $request)
	{
		if (!$this->getConfigData('active')) {
			Mage::log('The '.$this->_code.' shipping method is not active.');
			return false;
		}
		$handling = $this->getConfigData('handling');
		$result = Mage::getModel('shipping/rate_result');
		$method = Mage::getModel('shipping/rate_result_method');
		$items = Mage::getModel('checkout/session')->getQuote()->getAllItems();
		if (count($items) >= $this->getConfigData('minimum_item_limit')) {
			$code = $this->getConfigData('over_minimum_code');
			$title = $this->getConfigData('over_minimum_title');
			$price = $this->getConfigData('over_minimum_price');
		}
		else {
			$code = $this->getConfigData('under_minimum_code');
			$title = $this->getConfigData('under_minimum_title');
			$price = $this->getConfigData('under_minimum_price');
		}
		$method->setCarrier($this->_code);
		$method->setCarrierTitle($this->getConfigData('title'));
		$method->setMethod($code);
		$method->setMethodTitle($title);
		$method->setPrice($price + $handling);
		$result->append($method);
		return $result;
	}
}

这段代码首先定义shipping method的code,之后再实例化抽象方法collectRates,你可以从Mage_Shipping_Model_Carrier_Abstract找到,系统默认的写法有做Mage_Shipping_Model_Carrier_Interface的接口 两个类里面都有collectRates这个方法。这个方法具体用来判断模块是否激活 如果没有就写到日志中。如果有的话取得各个参数的值,最主要取得订单中产品的数量 如果这个值大于后台设定的数目 运费就变成后台设置的某个值,若小于,后台又可以设置另外一个值。所以功能上是用来判断客户下单中买了几个产品 按产品数量来定义运费 这个非常有趣,可以再根据自己网站需求进行修改。当然那些参数会在第四步写文件。


4.app\code\local\Yip\CustomShipping\etc 到这里新建一个system.xml吧,代码如下:

<config>
	<sections>
		<carriers>
			<groups>
				<customshipping translate="label" module="shipping">
					<label>YipEc internetional Shipping inc.</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>
					<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>0</show_in_store>
                        </active>
						<minimum_item_limit translate="label">
							<label>Minimum item limit</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>
						</minimum_item_limit>
						<over_minimum_code translate="label">
							<label>Minimum item code</label>
							<frontend_type>text</frontend_type>
							<sort_order>3</sort_order>
							<show_in_default>1</show_in_default>
							<show_in_website>1</show_in_website>
							<show_in_store>1</show_in_store>
						</over_minimum_code>
						<over_minimum_title translate="label">
							<label>Minimum item title</label>
							<frontend_type>text</frontend_type>
							<sort_order>4</sort_order>
							<show_in_default>1</show_in_default>
							<show_in_website>1</show_in_website>
							<show_in_store>1</show_in_store>
						</over_minimum_title>
						<over_minimum_price translate="label">
							<label>Minimum item price</label>
							<frontend_type>text</frontend_type>
							<sort_order>5</sort_order>
							<show_in_default>1</show_in_default>
							<show_in_website>1</show_in_website>
							<show_in_store>1</show_in_store>
						</over_minimum_price>
						<under_minimum_code translate="label">
							<label>Under minimum code</label>
							<frontend_type>text</frontend_type>
							<sort_order>6</sort_order>
							<show_in_default>1</show_in_default>
							<show_in_website>1</show_in_website>
							<show_in_store>1</show_in_store>
						</under_minimum_code>
						<under_minimum_title translate="label">
							<label>Under minimum title</label>
							<frontend_type>text</frontend_type>
							<sort_order>7</sort_order>
							<show_in_default>1</show_in_default>
							<show_in_website>1</show_in_website>
							<show_in_store>1</show_in_store>
						</under_minimum_title>
						<under_minimum_price translate="label">
							<label>Under minimum price</label>
							<frontend_type>text</frontend_type>
							<sort_order>8</sort_order>
							<show_in_default>1</show_in_default>
							<show_in_website>1</show_in_website>
							<show_in_store>1</show_in_store>
						</under_minimum_price>
						<handling_type translate="label">
							<label>Calculate Handling Fee</label>
							<frontend_type>select</frontend_type>
							<source_model>shipping/source_handlingType</source_model>
							<sort_order>9</sort_order>
							<show_in_default>1</show_in_default>
							<show_in_website>1</show_in_website>
							<show_in_store>0</show_in_store>
						</handling_type>
						<handling_action translate="label">
							<label>Handling Applied</label>
							<frontend_type>select</frontend_type>
							<source_model>shipping/source_handlingAction</source_model>
							<sort_order>10</sort_order>
							<show_in_default>1</show_in_default>
							<show_in_website>1</show_in_website>
							<show_in_store>0</show_in_store>
						</handling_action>
						<handling_fee translate="label">
							<label>Handling fee</label>
							<frontend_type>text</frontend_type>
							<sort_order>11</sort_order>
							<show_in_default>1</show_in_default>
							<show_in_website>1</show_in_website>
							<show_in_store>1</show_in_store>
						</handling_fee>
						<sallowspecific translate="label">
							<label>Ship to applicable countries</label>
							<frontend_type>select</frontend_type>
							<sort_order>12</sort_order>
							<frontend_class>shipping-applicable-country</frontend_class>
							<source_model>adminhtml/system_config_source_shipping_allspecificcountries</source_model>
							<show_in_default>1</show_in_default>
							<show_in_website>1</show_in_website>
							<show_in_store>1</show_in_store>
						</sallowspecific>
						<specificcountry translate="label">
							<label>Ship to Specific countries</label>
							<frontend_type>multiselect</frontend_type>
							<sort_order>13</sort_order>
							<source_model>adminhtml/system_config_source_country</source_model>
							<show_in_default>1</show_in_default>
							<show_in_website>1</show_in_website>
							<show_in_store>1</show_in_store>
						</specificcountry>
						<showmethod translate="label">
							<label>Show method if not applicable</label>
							<frontend_type>select</frontend_type>
							<sort_order>14</sort_order>
							<source_model>adminhtml/system_config_source_yesno</source_model>
							<show_in_default>1</show_in_default>
							<show_in_website>1</show_in_website>
							<show_in_store>1</show_in_store>
						</showmethod>
					</fields>
				</customshipping>
			</groups>
		</carriers>
	</sections>
</config>

这个xml配置文件就可以用来配置后台显示情况,标题啊,加什么模块用到的参数啊,参数的类型啊,这个文件相信很容易读懂,请细细看看。


5.演示,写好了 刷缓存,后台就能看到模块了,并且当你结账的时候买了2件产品以上 会有比较优惠的价格,如果是1件,那么又是另外一个价格,CSDN图的限制 就不多发图了 大家自己琢磨:




这个是不是很有趣的一个做法,好好努力吧~

That's it~ Happy coding~


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值