Programmatically: How to use Magento's custom shipping carrier integration mechanism for custom shipping options
Step 1: Create a custom module
Create a new directory MyCompany/MyCustomShipping in the app/code directory and add the following files:
etc/module.xml:
<?xml version="1.0"?>
<module name="MyCompany_MyCustomShipping" setup_version="1.0.0">
<config>
<modules>
<MyCompany_MyCustomShipping>
<version>1.0.0</version>
</MyCompany_MyCustomShipping>
</modules>
</config>
</module>
etc/config.xml:
<?xml version="1.0"?>
<config>
<modules>
<MyCompany_MyCustomShipping>
<version>1.0.0</version>
</MyCompany_MyCustomShipping>
</modules>
<global>
<carriers>
<mycustomshipping_carrier>
<class>MyCompany_MyCustomShipping_Model_Carrier_MyCustomShipping</class>
</mycustomshipping_carrier>
</carriers>
</global>
</config>
Model/Carrier/MyCustomShipping.php:
class MyCompany_MyCustomShipping_Model_Carrier_MyCustomShipping extends Mage_Shipping_Model_Carrier_Abstract
{
const CODE = 'mycustomshipping';
public function collectRates(Mage_Shipping_Model_Rate_Request_Interface $request)
{
// Implement your custom logic here to calculate the shipping rates
// For example, you can use APIs or web services to fetch rates
// ...
// Return an array of rates
$rates = array();
$rates[] = new Mage_Shipping_Model_Shipping_Rate(
array(
'carrier' => self::CODE,
'carrier_code' => self::CODE,
'title' => 'My Custom Shipping',
'price' => 10.99, // or any other price
'estimate' => '1-3 business days'
)
);
return $rates;
}
public function getAllowedMethods()
{
return array('mycustomshipping');
}
}
Step 2: Configure the carrier in the Magento admin
In the Magento admin, go to System > Configuration > Shipping > Carriers and add a new carrier with the code mycustomshipping. Set the Method to mycustomshipping and the Title to your desired title.
Step 3: Test the custom shipping carrier
In the Magento frontend, go to the checkout page and select the custom shipping carrier as the shipping method. You should see the rate and estimated delivery time.