Create a Custom Shipping Method in Magento
Magento provides shipping libraries for all popular shipping carriers but sometimes you may need to implement your own custom logic, in which case you’ll need to create a custom shipping method as per your requirements. In this tutorial, I will use Magento’s powerful shipping-method code abstraction to create a shipping carrier. I’ll create a simple custom shipping module named “Customshipping”. Here’s the list of files required for the custom shipping setup.
a) app/etc/modules/Prashant_Customshipping.xml: It’s a file which is used to enable our custom shipping module.
b) app/code/local/Prashant/Customshipping/etc/config.xml: It’s a module configuration file in which I’ll declare custom shipping method using certain tags as per the Magento conventions.
c) app/code/local/Prashant/Customshipping/etc/system.xml: It’s a file which is used for the admin configuration for custom shipping method.
d) app/code/local/Prashant/Customshipping/Model/Carrier.php: It’s a model file which is used for the calculation of shipping rates.
e) app/code/local/Prashant/Customshipping/Helper/Data.php: It’s a helper file used by the Magento translation system or where you can create your own function.
First, we need to create a module enabler file. Create a file “app/etc/modules/Prashant_Customshipping.xml” and paste the following contents in that file.
<?xml version="1.0"?> <config> <modules> <Prashant_Customshipping> <active>true</active> <codePool>local</codePool> <depends> <Mage_Shipping /> </depends> </Prashant_Customshipping> </modules> </config>
Notice the dependency on the shipping module. This ensures that our Prashant_Customshipping module will load after the Mage Shipping module, and it will throw an error if the Mage Shipping module has been disabled.
Now next steps, we need to create a module configuration file. Create “app/code/local/Prashant/Customshipping/etc/config.xml” and paste the following contents in that file.
<?xml version="1.0"?> <config> <modules> <Prashant_Customshipping> <version>1.0</version> </Prashant_Customshipping> </modules> <global> <models> <prashant_customshipping> <class>Prashant_Customshipping_Model</class> </prashant_customshipping> </models> <helpers> <prashant_customshipping> <class>Prashant_Customshipping_Helper</class> </prashant_customshipping> </helpers> </global> <default> <carriers> <prashant_customshipping> <active>1</active> <sallowspecific>1</sallowspecific> <model>prashant_customshipping/carrier</model> <title>Prashant Shipping Method</title> <name>Prashant Shipping Carrier</name> <price>55.00</price> </prashant_customshipping> </carriers> </default> </config>
Now I’ll create a “system.xml” file which provides the admin configuration for our custom shipping method. Create a file under “app/code/local/Prashant/Prashant_Customshipping/etc/system.xml” and paste below contents.
<?xml version="1.0" encoding="UTF-8"?> <config> <sections> <carriers> <groups> <prashant_customshipping translate="label"> <label>Prashant Custom Shipping Method</label> <sort_order>10</sort_order> <show_in_default>1</show_in_default> <show_in_website>0</show_in_website> <show_in_store>0</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>0</show_in_website> <show_in_store>0</show_in_store> </active> <title translate="label"> <label>Shipping Method Name</label> <frontend_type>text</frontend_type> <sort_order>20</sort_order> <show_in_default>1</show_in_default> <show_in_website>0</show_in_website> <show_in_store>0</show_in_store> </title> <sallowspecific translate="label"> <label>For selected countries only</label> <frontend_type>select</frontend_type> <frontend_class>shipping-applicable-country</frontend_class> <source_model>adminhtml/system_config_source_shipping_allspecificcountries</source_model> <sort_order>30</sort_order> <show_in_default>1</show_in_default> <show_in_website>0</show_in_website> <show_in_store>0</show_in_store> </sallowspecific> <specificcountry translate="label"> <label>Ship to Specific Countries</label> <frontend_type>multiselect</frontend_type> <sort_order>31</sort_order> <source_model>adminhtml/system_config_source_country</source_model> <show_in_default>1</show_in_default> <show_in_website>0</show_in_website> <show_in_store>0</show_in_store> <can_be_empty>1</can_be_empty> </specificcountry> </fields> </prashant_customshipping> </groups> </carriers> </sections> </config>
Now let’s create a model file under “app/code/local/Prashant/Customshipping/Model/Carrier.php” with the following contents.
<?php // app/code/local/Prashant/Customshipping/Model class Prashant_Customshipping_Model_Carrier extends Mage_Shipping_Model_Carrier_Abstract implements Mage_Shipping_Model_Carrier_Interface { protected $_code = 'prashant_customshipping'; public function collectRates(Mage_Shipping_Model_Rate_Request $request) { $result = Mage::getModel('shipping/rate_result'); $result->append($this->_getDefaultRate()); return $result; } public function getAllowedMethods() { return array( 'prashant_customshipping' => $this->getConfigData('name'), ); } protected function _getDefaultRate() { $rate = Mage::getModel('shipping/rate_result_method'); $rate->setCarrier($this->_code); $rate->setCarrierTitle($this->getConfigData('title')); $rate->setMethod($this->_code); $rate->setMethodTitle($this->getConfigData('name')); $rate->setPrice($this->getConfigData('price')); $rate->setCost($this->getConfigData('price')); return $rate; } } ?>
Now create helper file “”app/code/local/Prashant/Customshipping/Helper/Data.php”” and paste below contents into it.
<?php class Prashant_Customshipping_Helper_Data extends Mage_Core_Helper_Abstract { } ?>
That’s all. Enjoy.
In case you still need some help regarding custom shipping method or checkout or your website then fell free to contact me.