Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

Write a Custom Shipping Code for your Magento E-Store

Author: Deepa Ranganathan
by Deepa Ranganathan
Posted: Apr 10, 2015

There are two custom shipping methods for any e-Store based on Magento- standard shipping and express shipping. For the two shipping methods to be active there is a weight benchmark that the items added to cart should adhere to.

The first step is to call a carrier and the second step is to request the rates for the different carriers. The carriers are represented with the class Mage_Shipping_Model_Carrier_Abstract. To request the rates, the class defined is Mage_Shipping_Model_Rate_Request, and the value returned would be Mage_Shipping_Model_Rate_Result

The code used to collect the different rates from the different carriers is below. The whole process occurs within Mage_Shipping_Model_Shipping::CollectRates()

$carriers = Mage::getStoreConfig('carriers', $storeId);

foreach ($carriers as $carrierCode => $carrierConfig)

{

$this->collectCarrierRates($carrierCode, $request);

}

With the collectcarrierrates() function, you will gain information on the different carriers available for shipping within a particular country, and accordingly the rates would be sent out in the result pane. Now, that you have a general idea of how the function works, let's see how the custom shipping code is written.

Your first step would be to create a new module by the name Mage_Shipping

Once you have implemented the standard module configuration method, you will need to implement config.xml too

...

...

1

test_shipping/carrier

test Shipping Carrier

10

0

1

...

...

The three config entries functions namely entries active, sallowspecific and express_max_items will be used in the coding.

Let's specify a carrier for the store

Test_Shipping_Model_Carrier. Extend this to Mage_Shipping_Model_Carrier_Abstract and then implement the class Mage_Shipping_Model_Carrier_Interface so that your Magento store can use this class to write custom shipping codes

class Test_Shipping_Model_Carrier

extends Mage_Shipping_Model_Carrier_Abstract

implements Mage_Shipping_Model_Carrier_Interface

{

protected $_code = 'test_shipping';

Now implement getAllowedMethods() to the interface. The value returned will be an array of all the key value pairs for the different custom shipping methods, as discussed earlier

public function getAllowedMethods()

{

return array (

'standard' => 'Standard delivery',

'express' => 'Express delivery',

);

}

The rates for the different carriers and custom shipping methods will be received using collectRates() function. This function will also return the value of the rate that is allowed for a specific request

public function collectRates(Mage_Shipping_Model_Rate_Request $request)

{

/** @var Mage_Shipping_Model_Rate_Result $result */

$result = Mage::getModel('shipping/rate_result');

/** @var Inchoo_Shipping_Helper_Data $expressMaxProducts */

$expressMaxWeight = Mage::helper('inchoo_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;

}

To determine the express rate, the weight of the products in the cart is compared with the benchmarked weight present within the config code. The standard rate is available for view at all times. Each rate is then added individually by passing the object Mage_Shipping_Model_Rate_Result_Method to append()

To get the rates in the results you can use functions like _getExpressRate() and _getStandardRate()

protected function _getStandardRate()

{

/** @var Mage_Shipping_Model_Rate_Result_Method $rate */

$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;

}

With this code the custom shipping class gets to work and returns the rates for individual shipping methods. Now add admin configuration to system.xml and you will be done with writing custom shipping codes for your store

...

...

...

...

select

shipping-applicable-country

adminhtml/system_config_source_shipping_allspecificcountries

...

...

multiselect

adminhtml/system_config_source_country

...

...

Functions like active, sallowspecific, specificcountry and title are auto-handled with Magento. When you finish adding the code to admin, your job of writing the custom code is complete and the shipping method will appear at the checkout page.

Note: Don't forget to take a backup of your database and store before you perform this coding

About the Author

Deepa is a technical writer with Semaphore Software.

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Deepa Ranganathan

Deepa Ranganathan

Member since: Mar 29, 2015
Published articles: 42

Related Articles