Magento: programmatically set free shipping

Magento: programmatically set free shipping

October 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: programmatically set free shipping

Setting Free Shipping Programmatically in Magento

In Magento, setting free shipping for a quote involves manipulating the quote object’s shipping address properties. Let me guide you through this process.

Understanding the Quote Object

The Mage::helper('checkout/cart')->getCart()->getQuote() statement retrieves the current shopping cart quote. This is an instance of the Mage_Sales_Model_Quote class, which encapsulates all relevant information about the customer’s cart, including items, shipping addresses, and applied discounts.

Setting Free Shipping

Your attempt to set free shipping using $quote->getShippingAddress()->setFreeShipping(1); looks correct in principle. However, several factors might be preventing this from working as expected:

  1. Quote Status: Ensure that the quote is not yet converted into an order (i.e., it’s still pending). Once a quote is converted to an order, you cannot change its shipping details programmatically.

  2. Shipping Method Availability: The free shipping method must exist and be available for the given quote. Check if the freeshipping_freeshipping method is configured correctly in your system.

  3. Event Timing: If you’re trying to set this from an event observer, make sure the event is triggered at the right stage of the cart modification process. In your case, you’ve mentioned the events checkout_cart_update_items_after, checkout_cart_product_add_after, and sales_quote_remove_item. These events occur after a cart item has been added or removed.

Disabling Free Shipping as a Checkout Option

To disable free shipping as an option during checkout, you need to set the shipping method directly on the quote’s shipping address:

$address = $quote->getShippingAddress();
$address->setShippingMethod('freeshipping_freeshipping');

This code sets the freeshipping_freeshipping method as the active one for the specified quote. However, this won’t actually prevent it from appearing in the checkout process.

Preventing Free Shipping from Appearing

To prevent free shipping from appearing as an option during checkout, you’ll need to modify the checkout behavior by extending or overriding Magento’s core functionality:

  1. Extend the Checkout Controller: You can create a custom checkout controller that inherits from Mage_Checkout_Controller_Action and overrides the index action.

  2. Override the Shipping Method Collection: In your extension, you can override the _getAllowedMethods() method in the Mage_Shipping_Model_Rate_Request class to filter out or disable the free shipping method based on certain conditions (e.g., cart total).

Remember that modifying core Magento files is generally discouraged due to upgrade compatibility issues. It’s recommended to use extensions, which allow you to override behavior without touching core files.

Conclusion

Setting free shipping programmatically in Magento involves a deep understanding of how quotes and shipping methods work within the system. While your initial approach was on the right track, successful implementation requires careful consideration of quote status, event timing, and checkout behavior.

I encourage you to explore the official Magento Developer Documentation for more detailed information about working with quotes and shipping methods in Magento 2. Additionally, consider reaching out to our team at Laravel Company if you need expert consultation or custom development services tailored to your specific e-commerce needs.

Happy coding!

Checkout Catalog Extensions Configuration

Our network