当前位置:编程学习 > 网站相关 >>

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>
       &nbs

补充:Web开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,