Magento - adding special shipping rules?

Magento - adding special shipping rules?

September 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - adding special shipping rules?

Dynamic Shipping Rules in Magento: A Comprehensive Guide

As an experienced Magento developer, I often encounter clients who require complex shipping rule sets tailored to their specific business needs. In this post, we’ll explore how to implement the desired tiered pricing model for your client’s shipping costs.

Understanding the Client’s Requirements

Your client wants a progressive pricing structure based on the number of items in each order:

  • Surcharge $5 for 1 item
  • Surcharge $7 for 2 items
  • Surcharge $10 for 3 or more items

At first glance, it may seem that Magento’s built-in shipping rates aren’t well-suited to handle such a scenario. However, with some creative application of core features and custom code, we can achieve the desired outcome.

Step 1: Setting Up Flat Rate Shipping

The first step is to enable flat rate shipping for your store. Navigate to Stores > Configuration > Sales > Shipping Methods. Enable the ‘Flat Rate’ option and set it as your default carrier.

Step 2: Creating Custom Shipping Rules

To implement the tiered pricing, we’ll need to create a custom module that extends Magento’s core shipping functionality. Here’s an outline of the steps involved:

  1. Create a new module folder under app/code/Your_Namespace/

    • namespace = Your_Company_Name
  2. Inside this folder, create the following:

    • The app/code/Your_Namespace/ShippingRates/etc/config.xml file
    • The app/code/Your_Namespace/ShippingRates/Model/ directory
  3. In config.xml, configure your module to observe the sales_model_service_quote_address_item_save_before event.

Step 3: Calculating Shipping Costs

In the event observer, you can use Magento’s core quote model to calculate the number of items and apply the appropriate surcharge based on the client’s requirements. Here’s a code snippet:

$itemsCount = $observer->getEvent()->getQuoteItem()->getQty();
switch ($itemsCount) {
    case 1:
        $shippingAmount = 5;
        break;
    case 2:
        $shippingAmount = 7;
        break;
    default:
        $shippingAmount = 10;
        break;
}

Step 4: Applying the Shipping Surcharge

Once you have calculated the shipping amount, update the quote item’s address with the new total:

$quoteItem->getQuote()->setShippingAmount($shippingAmount);
$quoteItem->getQuote()->save();

Step 5: Testing and Deployment

After implementing the custom code, thoroughly test your setup to ensure that the correct surcharges are applied for each order scenario. Once tested successfully, deploy it to your client’s production environment.

Conclusion

In this post, we explored how to implement a tiered shipping rate structure using Magento’s core functionality and some custom development. By following the outlined steps and leveraging Magento’s event observation system, you can provide your clients with the flexibility they need for their unique shipping requirements.

Remember to always follow best practices when developing custom modules:

  • Use proper version control systems
  • Follow a consistent coding style
  • Test thoroughly before deployment
  • Document all changes for future reference

For more detailed guidance on Magento development and best practices, feel free to explore our comprehensive resources at LaravelCompany.com.

Extensions Configuration Orders Pricing

Our network