On my going Project a client requirement was adding custom shipping method so I was trying to solve the problem at last got a solution and it so i liked to share it hope it will be helpful for you.
In this article I will Discuss how to write custom shipping method in Magento commerce. First we need to write our shipping method, it’s a class in folder app/code/core/Mage/Shipping/Model/Carrier/.
I called it Inchoocustom.php and it’s based on Flat rate shipping method but you can developed your own class
I called it Inchoocustom.php and it’s based on Flat rate shipping method but you can developed your own class
getConfigFlag(‘active’)) {
return false;
}
$freeBoxes = 0;
if ($request->getAllItems()) {
foreach ($request->getAllItems() as $item) {
if ($item->getFreeShipping() && !$item->getProduct()->isVirtual()) {
$freeBoxes+=$item->getQty();
}
}
}
$this->setFreeBoxes($freeBoxes);
$result = Mage::getModel(‘shipping/rate_result’);
if ($this->getConfigData(‘type’) == ‘O’) { // per order
$shippingPrice = $this->getConfigData(‘price’);
} elseif ($this->getConfigData(‘type’) == ‘I’) { // per item
$shippingPrice = ($request->getPackageQty() * $this->getConfigData(‘price’)) – ($this->getFreeBoxes() * $this->getConfigData(‘price’));
} else {
$shippingPrice = false;
}
$shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);
if ($shippingPrice !== false) {
$method = Mage::getModel(‘shipping/rate_result_method’);
$method->setCarrier(‘inchoocustom’);
$method->setCarrierTitle($this->getConfigData(‘title’));
$method->setMethod(‘inchoocustom’);
$method->setMethodTitle($this->getConfigData(‘name’));
if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) {
$shippingPrice = ‘0.00’;
}
$method->setPrice($shippingPrice);
$method->setCost($shippingPrice);
$result->append($method);
}
return $result;
}
public function getAllowedMethods()
{
return array(‘inchoocustom’=>$this->getConfigData(‘name’));
}
}
?>
Pay attention on following lines:
$method->setCarrier(‘inchoocustom’);
$method->setCarrierTitle($this->getConfigData(‘title’));
$method->setMethod(‘inchoocustom’);
$method->setMethodTitle($this->getConfigData(‘name’));
They determine how we will set up our method. Keyword is ‘inchoocustom’. In folder app/code/core/Mage/Shipping/etc/ we need to edit
system.xml and config.xml in order to enable our new shipping method.
system.xml:
Inchoo Custom Shipping Method
text
2
1
1
1
Enabled
select
adminhtml/system_config_source_yesno
1
1
1
0
Method name
text
3
1
1
1
Price
text
5
1
1
0
Calculate Handling Fee
select
shipping/source_handlingType
7
1
1
0
Handling Fee
text
8
1
1
0
Sort order
text
100
1
1
0
Title
text
2
1
1
1
Type
select
adminhtml/system_config_source_shipping_flatrate
4
1
1
0
Ship to applicable countries
select
90
shipping-applicable-country
adminhtml/system_config_source_shipping_allspecificcountries
1
1
0
Ship to Specific countries
multiselect
91
adminhtml/system_config_source_country
1
1
0
Show method if not applicable
select
92
adminhtml/system_config_source_yesno
1
1
0
Displayed Error Message
textarea
80
1
1
1
config.xml:
0 0 shipping/carrier_inchoocustom Inchoo Custom Shipping Method 5.00 Inchoo Custom Shipping Method I This shipping method is currently unavailable. If you would like to ship using this shipping method, please contact us. F
Now we need to enable our new shipping method in admin panel (System->Configuration->Shipping Methods) And we have our custom shipping method. Enjoy coding.