In magento you can add custom shipping in your website by writing some custom code , lets first go through how shipping work in magento ,
When magento looks for available shipping methods, Magento first collects all available shipping methods. Each shipping method is represented by a class that extends Mage_Shipping_Model_Carrier_Abstract.
After list of shipping method, shipping information(implemented as Mage_Shipping_Model_Rate_Request) is sent to carrier in order to retrieve all available rates provided by given shipping method, represented as Mage_Shipping_Model_Rate_Result.
This process happens in Mage_Shipping_Model_Shipping::collectRates() as listed out in code below:
$carriers = Mage::getStoreConfig('carriers', $storeId);
foreach ($carriers as $carrierCode => $carrierConfig) {
$this->collectCarrierRates($carrierCode, $request);
}
Here collectCarrierRates() will check all condition like is carrier enabled in admin, is it available for requested country, etc and eventually triggers collectRates() function of your class, which we will implement later.
That is general outline of what is going on behind the scenes. We are now ready to write some code that will fit nicely into everything explained above. First thing you will need to do, is create new module which depends on Mage_Shipping. Besides standard module configuration, you will have following inside your config.xml:
And this is how the general flow how shipping methods comes into play ,now we will create a custom shipping module
1
custom_shipping/carrier
Custom Shipping Carrier
10
0
1
entries like active, sallowspecific and express_max_items are config entries. We will start with model entry. You wil see that carrier will be represented with Custom_Shipping_Model_Carrier, so lets implement that class.
class Custom_Shipping_Model_Carrier extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{
protected $_code = 'custom_shipping';
interface requires us to implement getAllowedMethods() that will returns array of key-value pairs of all available methods
public function getAllowedMethods()
{
return array(
'standard' => 'Standard delivery',
'express' => 'Express delivery',
);
}
rates are collected by calling collectRates(). This function takes shipping information as parameter, and returns all available rates
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
$result = Mage::getModel('shipping/rate_result');
/** @var Custom_Shipping_Helper_Data $expressMaxProducts */
$expressMaxWeight = Mage::helper('custom_shipping')->getExpressMaxWeight();
$expressAvailable = true;
foreach ($request->getAllItems() as $item) {
if ($item->getWeight() > $expressMaxWeight) {
$expressAvailable = false;
}
}
if ($expressAvailable) {
$result->append($this->)getExpressRate());
}
$result->append($this->_getStandardRate());
return $result;
}
Weight of all products in cart are compared to value stored in config, which determines availability of our express rate. Our standard rate is always available. Each rate is added by passing Mage_Shipping_Model_Rate_Result_Method object to append() of our result object. And we get those rate objects by calling _getExpressRate() and _getStandardRate(),
protected function _getStandardRate()
{
$rate = Mage::getModel('shipping/rate_result_method');
$rate->setCarrier($this->_code);
$rate->setCarrierTitle($this->getConfigData('title'));
$rate->setMethod('large');
$rate->setMethodTitle('Standard delivery');
$rate->setPrice(1.23);
$rate->setCost(0);
return $rate;
}
now we will be adding admin configuration through system.xml
<sections>
<carriers>
<groups>
<custom_shipping translate="label">
<fields>
<active translate="label">
</active>
<title translate="label">
</title>
<sallowspecific translate="label">
<frontend_type>select</frontend_type>
<frontend_class>shipping-applicable-country</frontend_class> <source_model>adminhtml/system_config_source_shipping_allspecificcountries</source_model>
</sallowspecific>
<specificcountry translate="label"> <br>
<frontend_type>multiselect</frontend_type>
<source_model>adminhtml/system_config_source_country</source_model>
</specificcountry>
<express_max_weight translate="label">
</express_max_weight>
</fields>
</custom_shipping>
</groups>
Refrences
http://inchoo.net/magento/custom-shipping-method-in-magento/
0 Comment(s)