create a shipping Method Module In Magento

Shipping Method ModuleMagento is one of the best and well-to-do e-commerce platforms where you can find quite a lot of shipping methods. On the other hand, when you are having big e-commerce sites you may need to build up your own custom shipping methods.

Generating a custom shipping method is not so difficult; you just have to get all the necessary information regarding various functions that have to be use inside the shipping method class to put in various configuration selections.

Every shipping method can be prepared as separate module but if some methods could be used together or have same functionality than it can be combined in the same module.

Make sure that app/code/local is in PHP‘s include_path. For doing this, you need to carry out the following code:

<?php echo get_include_path();?>

Be certain you place this code anywhere after Magento was loaded.
Configuration:

You need to form app/code/local/YourCompany/NewModule/etc/config.xml:
<?xml version=”1.0″?>
<config>
<modules>
<!– declare module’s version information –>
<YourCompany_NewModule>
<!– this version number will be used for database upgrades –>
<version>0.1.0</version>
</YourCompany_NewModule>
</modules>
<global>
<!– declare model group for new module –>
<models>
<!– model group alias to be used in Mage::getModel() –>
<newmodule>
<!– base class name for the model group –>
<class>YourCompany_NewModule_Model</class>
</newmodule>
</models>
<!– declare resource setup for new module –>
<resources>
<!– resource identifier –>
<newmodule_setup>
<!– specify that this resource is a setup resource and used for upgrades –>
<setup>
<!– which module to look for install/upgrade files in –>
<module>YourCompany_NewModule</module>
</setup>
<!– specify database connection for this resource –>
<connection>
<!– do not create new connection, use predefined core setup connection –>
<use>core_setup</use>
</connection>
</newmodule_setup>
</resources>
</global>
</config>

Now, create app/etc/modules/YourCompany_NewModule.xml:

<config>
<!– … –>
<modules>
<!– … –>
<!– declare YourCompany_NewModule module –>
<YourCompany_NewModule>
<active>true</active>
<codePool>local</codePool>
</YourCompany_NewModule>
<!– … –>
</modules>
<!– … –>
</config>

Now, create app/code/local/YourCompany/NewModule/Model/Carrier/ShippingMethod.php:

<?php

/**
* Our test shipping method module adapter
*/
class YourCompany_NewModule_Model_Carrier_ShippingMethod extends Mage_Shipping_Model_Carrier_Abstract
{
/**
* unique internal shipping method identifier
*
* @var string [a-z0-9_] */
protected $_code = ‘newmodule’;
/**
* Collect rates for this shipping method based on information in $request
*
* @param Mage_Shipping_Model_Rate_Request $data* @return Mage_Shipping_Model_Rate_Result
*/
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
// skip if not enabled
if (!Mage::getStoreConfig(‘carriers/’.$this->_code.’/active’)) {
return false;
}
/**
* here we are retrieving shipping rates from external service
* or using internal logic to calculate the rate from $request
* you can see an example in Mage_Usa_Model_Shipping_Carrier_Ups::setRequest()
*/
// get necessary configuration values
$handling = Mage::getStoreConfig(‘carriers/’.$this->_code.’/handling’);
// this object will be returned as result of this method
// containing all the shipping rates of this method
$result = Mage::getModel(‘shipping/rate_result’);
// $response is an array that we have
foreach ($response as $rMethod) {
// create new instance of method rate
$method = Mage::getModel(‘shipping/rate_result_method’);
// record carrier information
$method->setCarrier($this->_code);
$method->setCarrierTitle(Mage::getStoreConfig(‘carriers/’.$this->_code.’/title’));
// record method information
$method->setMethod($rMethod['code']);
$method->setMethodTitle($rMethod['title']);
// rate cost is optional property to record how much it costs to vendor to ship
$method->setCost($rMethod['amount']);
// in our example handling is fixed amount that is added to cost
// to receive price the customer will pay for shipping method.
// it could be as well percentage:
/// $method->setPrice($rMethod['amount']*$handling/100);
$method->setPrice($rMethod['amount']+$handling);
// add this rate to the result
$result->append($method);
}
return $result;
}
/**
* This method is used when viewing / listing Shipping Methods with Codes programmatically
*/
public function getAllowedMethods() {
return array($this->_code => $this->getConfigData(‘name’));
}
}

Now you need to facilitate your new shipping method in admin panel (System->Configuration->Shipping Methods).

The following two tabs change content below.
Harshal Shah is Ceo & Founder of Xhtmljunkies, Located in Gujarat, India, XHTML Junkies is one of the best companies that offer unique eCommerce solutions by the virtue of its dedicated professionals. Our professionals are extremely proficient in offering development services pertaining to eCommerce. You can find Harshal on and Twitter.

Latest posts by Harshal Shah (see all)