Magento custom price rules for product

Magento custom price rules for product

August 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento custom price rules for product

Creating Custom Price Rules for Products in Magento

Managing complex pricing structures is a common challenge faced by e-commerce businesses. I recently received a question from a Magento user who needed to set up custom price rules based on the quantity of a specific product sold. The requirements were quite unique:

  • If 0-100 units are sold, the price should be $100
  • For 200-500 units, the price jumps to $400
  • Between 500-1000 units, the price increases to $800
  • After 1000 units, the price is fixed at $1000

While Magento does offer built-in features for setting up tiered pricing, this specific scenario required a more custom approach. Let’s explore how you can implement this using Magento’s extensible architecture.

Step 1: Create a Custom Product Attribute

First, we need to create a new attribute that will store the total number of units sold for this product. Go to “Catalog > Attributes” and click on “New Attribute.”

Set the following properties:

  • Attribute Code: units_sold
  • Catalog Input Type for Store Owner: Text Field
  • Apply To: Simple Product

Click “Save Attribute.” This will create a new field in your products.

Step 2: Update Your Products

Go to your product catalog and edit the desired product. In the “General” tab, add this code to update the units_sold attribute every time an order is placed:

public function afterSave()
{
    $orderCollection = Mage::getModel('sales/order')
        ->getCollection()
        ->addAttributeToFilter('items', array(
            'like' => '%"sku": "' . $this->getSku() . '"',
            'simple' => true
        ));

    $totalUnitsSold = 0;
    foreach ($orderCollection as $order) {
        foreach ($order->getAllItems() as $item) {
            if ($item->getSku() == $this->getSku()) {
                $totalUnitsSold += $item->getQtyOrdered();
            }
        }
    }

    $this->setData('units_sold', $totalUnitsSold);
}

This PHP script queries the database for all orders that contain this specific product and calculates the total units sold. It then sets this value in the units_sold attribute.

Step 3: Create a Custom Price Model

Next, we need to create a custom price model that will calculate the final price based on the units_sold value. This involves extending Magento’s core price models and overriding specific methods.

To do this, you would create a new module and implement a class similar to this:

class Laravelcompany_CustomPrice_Model_Sales_Order_Item extends Mage_Sales_Model_Order_Item {
    public function calculateRowTotal() {
        // Your custom pricing logic here
    }
}

In the calculateRowTotal method, you would use the units_sold value to determine which price tier applies.

Conclusion

Implementing complex pricing structures like this requires a solid understanding of Magento’s architecture and its extensibility features. By creating custom product attributes, updating your products programmatically after each order, and overriding core models, we can achieve highly customized pricing rules.

Remember, the key is to leverage Magento’s flexibility while maintaining a clean separation between your customizations and the core system. This allows for easier updates and better maintainability of your store.

For more detailed guides on extending Magento, check out our blog at https://laravelcompany.com/magento-dev-blog where we regularly share insights into Magento’s more advanced features.

Catalog Configuration Orders Pricing

Our network