Magento change shipping price based on products from category x?

Magento change shipping price based on products from category x?

September 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento change shipping price based on products from category x?

Magento: Changing Shipping Prices Based on Category-Specific Products

When it comes to managing shipping costs in Magento, you have robust options for creating complex pricing rules. However, the built-in promotions system does indeed have some limitations, particularly around adjusting the shipping price itself.

While you can create a promotion rule to match all products from category X and set them up for free shipping, directly modifying the shipping cost based on that category is not natively supported in Magento’s core functionality.

Understanding the Limitations

Magento’s promotions system operates primarily at the product level. It allows you to apply special prices or free shipping to individual items based on various conditions like price range, quantity, or other attributes. However, it does not provide a direct way to modify the shipping cost for an entire cart based on category composition.

A Custom Solution: Cart Price Rule

While Magento’s built-in features may seem restrictive in this case, there is a more advanced solution available through custom development or third-party extensions like Amasty shipping rules. Let’s explore how you can achieve this through a custom solution using Magento’s salesrule and salesrule_product entities.

  1. Create a new Cart Price Rule

First, navigate to the backend of your Magento store: Stores > Promotions > Shopping Cart Price Rules. Click on “Add New Rule”. Here, you’ll set up a rule that matches all products from category X and triggers when the cart total reaches a certain condition (e.g., contains only products from this category).

  1. Use the ‘Apply to Shipping Amount’ Option

In the rule settings, under ‘Actions’, look for the option “Apply to Shipping Amount”. This field accepts an amount or percentage that can be added to or subtracted from the shipping cost.

  1. Create a Custom Observer

To ensure your rule triggers only when all products in the cart are from category X, you’ll need to create a custom observer for the sales_quote_collect_totals event. In this observer, check if 100% of the items in the quote belong to category X and then apply or modify the shipping cost accordingly.

Example Code Snippet

Here’s a simplified example of how you might structure your observer:

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $quote = $observer->getQuote();
    $categoryId = 123; // Replace with your category ID

    $totalCategoryProducts = 0;
    foreach ($quote->getAllItems() as $item) {
        if ($item->getProduct()->getCategoryIds() === [$categoryId]) {
            $totalCategoryProducts++;
        }
    }

    if ($totalCategoryProducts == count($quote->getAllItems())) {
        // All products are from the specified category
        $quote->setShippingAmount(5.00); // Set a fixed shipping amount
    }
}

Conclusion

While Magento’s core functionality may not provide a direct solution for changing shipping prices based on category composition, with some custom development or third-party extensions, you can achieve this advanced feature.

The key lies in creating a custom Cart Price Rule that triggers an event when the cart meets your specific criteria (in this case, containing only products from a particular category). By using a custom observer and modifying the shipping amount within it, you can implement the desired behavior.

Remember to thoroughly test any custom modifications in your development environment before deploying them to live stores. This approach ensures that your Magento store remains secure and compliant with best practices while providing the unique functionality your business requires.

To learn more about Magento’s event-driven architecture and how to create your own observers, I recommend exploring our guide on Magento 2 Event Observers at Laravel Company.

Checkout Catalog API Extensions

Our network