Magento: Setting custom shipping method programmatically

Magento: Setting custom shipping method programmatically

July 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Setting custom shipping method programmatically

Setting Custom Shipping Method Programmatically in Magento

Setting up a custom shipping method programmatically in Magento can be a bit challenging for beginners, especially when dealing with XML files and class structures. In this post, we’ll guide you through setting up a custom shipping method and adding it to an order programmatically.

Prerequisites

Before we begin, ensure your Magento environment is properly set up and you have access to the necessary files in app/etc/modules and app/code/local.

Step 1: Activate the Shipping Module

First, create or open the Extension_Shipping.xml file located at app/etc/modules. Add the following code:

<?xml version="1.0"?>
<config>
    <modules>
        <Extension_Shipping>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Shipping/>
            </depends>
        </Extension_Shipping>
    </modules>
</config>

This configuration tells Magento to include your custom shipping module.

Step 2: Configure the Shipping Method

Next, open or create the config.xml file in the following directory: app/code/local/Extension/Shipping/etc. Add the following code:

<?xml version="1.0"?>
<config>
    <modules>
        <Extension_Shipping>
            <version>0.1.0</version>
        </Extension_Shipping>
    </modules>
    <global>
        <models>
            <extension_shipping>
                <class>Extension_Shipping_Model</class>
            </extension_shipping>
        </models>
    </global>
    <default>
        <carriers>
            <extension_myrate>
                <active>1</active>
                <model>extension_shipping/carrier_myrate</model>
                <title>Extension Shipping</title>
                <name>Default Rate</name>
            </extension_myrate>
        </carriers>
    </default>
</config>

This configuration sets up the basic details of your custom shipping method.

Step 3: Create the Carrier Class

Now, open or create the Carrier/MyRate.php file in the following directory: app/code/local/Extension/Shipping/Model/Carrier. Add the following code:

<?php
class Extension_Shipping_Model_Carrier_MyRate
    extends Mage_Shipping_Model_Carrier_Abstract
    implements Mage_Shipping_Model_Carrier_Interface
{
    protected $_code = 'extension_myrate';
    protected $_isFixed = true;

    public function collectRates(Mage_Shipping_Model_Rate_Request $request)
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }

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

        $method = Mage::getModel('shipping/rate_result_method');
        $method->setCarrier('extension_myrate');
        $method->setCarrierTitle($this->getConfigData('title'));
        $method->setMethod('extension_myrate');
        $method->setMethodTitle($this->getConfigData('name'));
        $method->setPrice(5);
        $method->setCost(2);

        $result->append($method);

        return $result;
    }

    public function getAllowedMethods()
    {
        return array('extension_myrate' => $this->getConfigData('name'));
    }
}

This class defines your custom shipping method and its behavior.

Step 4: Setting the Shipping Method Programmatically

In your controller, you can now set the shipping method programmatically. Here’s an example:

$shippingAddress = $quote->getShippingAddress()->addData($addressData);
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
                ->setShippingMethod('extension_myrate')
                ->setPaymentMethod('checkmo');

In this code, replace $quote and $addressData with your actual quote object and address data.

Conclusion

Setting up a custom shipping method programmatically in Magento involves several steps: activating the module, configuring the shipping method, creating the carrier class, and setting the shipping method in your controller. Remember to clear your cache after making changes to the XML files.

If you’re still having trouble or need more guidance on custom Magento development, feel free to explore our other blog posts at https://laravelcompany.com/. We provide a wide range of tutorials and best practices for Magento developers.

Extensions Configuration Orders

Our network