Magento : Create Custom New Shipping Method

Hello guys, here we will create a new custom shipping method in magento.
For this, we need to create a module named Mynamespace_Shipping.
In default magento, shipping is handled through admin configuration and shown in frontend shopping cart page(sometimes on product page) and checkout page.

Step 1 – We will create first a module registration file named as Mynamespace_Shipping.xml
Location: app/etc/modules/Mynamespace_Shipping.xml
<?xml version=“1.0”?>
<config>

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

Step 2 – We will create our module configuration file to setup our custom shipping default setup
Location: app/code/local/Mynamespace/Shipping/etc/config.xml
<?xml version=“1.0”?>
<config>

<modules>
<Mynamespace_Shipping>
<version>1.0.0</version>
</Mynamespace_Shipping>
</modules>
<global>
<helpers>
<mynamespace_shipping>
<class>Mynamespace_Shipping_Helper</class>
</mynamespace_shipping>
</helpers>
<models>
<mynamespace_shipping>
<class>Mynamespace_Shipping_Model</class>
</mynamespace_shipping>
</models>
</global>
<default>
<carriers>
<cshipping>
<active>1</active>
<model>mynamespace_shipping/carrier_shipping</model>
<title>Custom Carrier Title</title>
<name>Custom Method Name</name>
<price>10.00</price>
</cshipping>
</carriers>
</default>
</config>

Step 3 – Now create admin system configuration for shipping in system.xml
Location: app/code/local/Mynamespace/Shipping/etc/system.xml
<?xml version=“1.0” encoding=“UTF-8”?>
<config>

<sections>
<carriers>
<groups>
<cshipping translate=“label” module=“mynamespace_shipping”>
<label>Custom Shipping Module</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>
<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>
<name translate=“label”>
<label>Method Name</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>
</name>
<price translate=“label”>
<label>Price</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>0</show_in_store>
</price>
<specificerrmsg translate=“label”>
<label>Displayed Error Message</label>
<frontend_type>textarea</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>
</specificerrmsg>
<sallowspecific translate=“label”>
<label>Ship to Applicable Countries</label>
<frontend_type>select</frontend_type>
<sort_order>6</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>0</show_in_store>
</sallowspecific>
<specificcountry translate=“label”>
<label>Ship to Specific Countries</label>
<frontend_type>multiselect</frontend_type>
<sort_order>7</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>0</show_in_store>
<can_be_empty>1</can_be_empty>
</specificcountry>
</fields>
</cshipping>
</groups>
</carriers>
</sections>
</config>

Step 4 – Create a default helper of our module to process system code and to avoid error in admin system configuration
Location: app/code/local/Mynamespace/Shipping/Helper/Data.php
<?php
class Mynamespace_Shipping_Helper_Data extends Mage_Core_Helper_Abstract
{
}
?>


Step 5 – Finally we need to create our model class to process our custom logic for shipping.
Location: app/code/local/Mynamespace/Shipping/Model/Carrier/Shipping.php
<?php
class Mynamespace_Shipping_Model_Carrier_Shipping extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{
protected $_code = ‘cshipping’;

public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
if (!Mage::getStoreConfig(‘carriers/’ . $this->_code . ‘/active’)) {
return false;
}

$result = Mage::getModel(‘shipping/rate_result’);
/* @var $result Mage_Shipping_Model_Rate_Result */
$result->append($this->_getCustomShippingRate());
return $result;
}

public function getAllowedMethods()
{
return array(‘mynamespace_shipping’ => $this->getConfigData(‘name’));
}

protected function _getCustomShippingRate()
{
$rate = Mage::getModel(‘shipping/rate_result_method’);
/* @var $rate Mage_Shipping_Model_Rate_Result_Method */

$rate->setCarrier($this->_code);
$rate->setCarrierTitle($this->getConfigData(‘title’));
$rate->setMethod($this->getConfigData(‘name’));
$rate->setMethodTitle($this->getConfigData(‘title’));
$rate->setPrice($this->getConfigData(‘price’));
$rate->setCost($this->getConfigData(‘price’));
return $rate;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s