Magneto Create a freight template

Source: Internet
Author: User
Tags php class

Magento system comes with about 7 kinds of freight: parity, freight table, free shipping, UPS, USPS, FedEx, DHL and so on. However, these still do not meet our needs, this time we need to create a shipping module to achieve. Creating a Shipping module is simple and requires inheriting mage_shipping_model_carrier_abstract abstract classes, implementing Mage_shipping_model_carrier_interface interface classes, This allows you to use the function collectrates to customize how freight is calculated. This allows you to create a plug-in that customizes the shipping method.

Add Module configuration information

First, add the module information and create the file 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 >

Add module configuration information, create file 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> 
Implement custom Shipping

Create the file app/code/local/hofan/ship/model/carrier/cm/dhl.php.

<?php class XBC_SHIP_MODEL_CARRIER_CM_DHL extends Mage_shipping_model_carrier_abstractimplements Mage_Shipping  _model_carrier_interface{protected $_code = ' CM_DHL ';  /** * Collect rates for the shipping method based on information in $request * * @p Aram Mage_shipping_model_rate_request $data * @return Mage_shipping_model_rate_result */public F Unction 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->getd            Estcountryid (); //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-&gt    ;_code=> $this->getconfigdata (' name '));} }
Add Background configuration

If you have completed the above steps, you can add a background configuration file. Create File 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</fro ntend_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&gt                            ; <source_model>adminhtml/system_config_source_yesno</source_model> <sort_order&gt                            ;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> <so                            Rt_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> &LT;SHOW_IN_STORE&GT;1&L                            t;/show_in_store> </title> <name translate= "label" > <label>method name</label> <frontend_type>text</frontend _type> <sort_order>40</sort_order> <show_in_defaul                            T>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> </name> <d                            Escription translate= "label" > <label>Description</label> <frontend_type>textarea</frontend_type> <sort_order>50</sort_order> <show_in_default>1</show_in_default> &LT;SHOW_IN_WEBSITE&GT;1&LT;/SHOW_IN_WEBSI                         Te> <show_in_store>1</show_in_store> </description> <sort_order translate= "label" > <label>sort order</label&                            Gt                            <frontend_type>text</frontend_type> <sort_order>100</sort_order> <show_in_default>1</show_in_default> <show_in_website>1& Lt;/show_in_website> <show_in_store>1</show_in_store> &lt ;/sort_order></fields></cm_dhl></groups></carriers> </sections></config>

The plug-in is tested on the Magento CE 1.6.2.

Magneto Create a freight template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.