Tag Archives: shipping Method Module

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.

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

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

<!– … –>
<!– … –>
<!– declare YourCompany_NewModule module –>
<!– … –>
<!– … –>

Now, create app/code/local/YourCompany/NewModule/Model/Carrier/ShippingMethod.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
// record method information
// rate cost is optional property to record how much it costs to vendor to ship
// 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);
// add this rate to the result
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).