Magento - buy 1 product get another free

Magento - buy 1 product get another free

July 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - buy 1 product get another free

Magento: Implementing a “Buy X Get Y Free” Promotion Across Categories

In this guide, we’ll explore how to set up a “buy one product from category A, get another free from category B” promotion in your Magento store. This feature isn’t available out-of-the-box, so we’ll need to leverage the built-in Shopping Cart Price Rules and add some custom code.

Understanding Magento’s Built-in Features

Magento provides powerful promotional tools, including the Shopping Cart Price Rule found under Promotions > Shopping Cart Price Rules. By default, this rule allows you to set up promotions like “buy X quantity of product A and get Y quantity of the same product free.”

However, your requirement involves providing a free product from a different category. Unfortunately, Magento doesn’t support this scenario directly.

Setting Up the Basic Promotion

To begin, let’s create the base promotion rule:

  1. Navigate to Promotions > Shopping Cart Price Rules.
  2. Click on “Add New Rule”.
  3. Set up your basic conditions and actions as desired.

For example, you might set it up like this:

  • If ALL of these conditions are TRUE: Customer Group = General
  • Then APPLY THE FOLLOWING DISCOUNT: Fixed amount discount to 0

Extending Magento’s Core Functionality

To achieve the desired “buy from category A, get free from category B” functionality, we need to extend Magento’s core. Here’s a high-level overview of what you’ll need to do:

  1. Create a new module: Follow our guide on creating Magento 2 modules to set up the basic structure.

  2. Override the applyRule method: In your module, override the Magento\SalesRule\Model\Rule class’s applyRule method.

  3. Add custom discount calculation logic: Inside this overridden method, you’ll need to add custom logic to identify products from category B that should be added as free items based on the purchase of a product from category A.

  4. Save and enable your module: Once you’ve written and tested your code, save it and enable your new module in Magento’s configuration.

Sample Code Snippet

Here’s a basic example of what the overridden applyRule method might look like:

public function applyRule($rule, $item)
{
    // Basic rule application logic here...

    // Add custom free item calculation
    if ($this->isCategoryAPurchase($item)) {
        $freeItems = $this->getFreeCategoryBItems();
        foreach ($freeItems as $freeItem) {
            $quoteItem = $quote->getItemByProduct($freeItem);
            if (!$quoteItem) {
                // Create a new free item
                $quoteItem = $quote->addItem(
                    $item->getOrder()->getQuoteId(),
                    $this->prepareFreeItem($freeItem)
                );
            }
        }
    }

    return true;
}

Conclusion

Implementing a “buy X get Y free” promotion across categories in Magento requires extending the core functionality. By creating a custom module and overriding the applyRule method, you can add the necessary logic to identify qualifying purchases and apply the desired discount.

Remember, this is a high-level overview, and the actual implementation will require careful testing to ensure it works as expected across various scenarios.

If you need further assistance with Magento development or have other questions about e-commerce platforms, feel free to reach out to our team at Laravel Company. We specialize in delivering high-quality Magento solutions tailored to your business needs.

Checkout Catalog Extensions Configuration

Our network