CommercePeer Brand Logo


Programmatically: Magento 2 Product Entity

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:


	
	
    
        
            
                1.0.0
            
        
    
	
	

etc/config.xml:


	
	
    
        
            1.0.0
        
    
    
        
            
                MyCompany_MyCustomShipping_Model_Carrier_MyCustomShipping
            
        
    
	
	

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.

LET’S WORK TOGETHER

We love working with Small Businesses, Retailers, Manufacturers. Our team is here to Help.

→ Schedule Free Consultant Now
Copyright © 2024 CommercePeer. All Rights Reserved.