Change subtotal with given price & quantity (custom attribute)

Change subtotal with given price & quantity (custom attribute)

October 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Change subtotal with given price & quantity (custom attribute)

Modifying Subtotal Based on Custom Price and Quantity in Magento

As an experienced developer, I understand that customizing Magento’s behavior to fit specific business needs can often require modifying core functionality. In your case, you want to dynamically adjust the subtotal based on a custom price and quantity attribute. Let me guide you through the process.

Understanding the Problem

In your scenario, you have a custom product with a specific price per unit (100g). The customer can input any quantity they desire. However, Magento’s current calculation method isn’t suitable for your requirements—you need to adjust the subtotal based on the actual weight (price * quantity).

Where to Make Modifications

To achieve this, you’ll need to use observers that listen to the sales_order_save_after and sales_quote_save_after events. These events trigger after an order or quote is saved in the system, providing a perfect opportunity to recalculate the subtotal.

Implementing the Observer Method

Let’s create a new observer class under your custom module:

class Xyz_Catalog_Model_Price_Observer
{
    public function apply_quantity_order($observer)
    {
        $order = $observer->getEvent()->getOrder();

        // Check if pricequantity attribute is set
        $pricequantity = $order->getData('pricequantity');

        if ($pricequantity != '') {
            // Calculate new subtotal based on pricequantity
            $new_sub_total = $order->getTotals() / 100;

            // Update the subtotal of the order
            $order->setGrandTotal($new_sub_total);
            $order->save();

        }

        return $this;
    }

    public function apply_quantity_quote($observer)
    {
        $quote = $observer->getEvent()->getQuote();

        // Check if pricequantity attribute is set on the quote
        $pricequantity = $quote->getData('pricequantity');

        if ($pricequantity != '') {
            // Calculate new subtotal based on pricequantity
            $new_sub_total = $quote->getTotals() / 100;

            // Update the subtotal of the quote
            $quote->setGrandTotal($new_sub_total);
            $quote->save();

        }

        return $this;
    }
}

Configuring the Observer

You’ll need to configure your observer in the config.xml file:

<?xml version="1.0"?>
<config>
  <global>
    <models>
        <xyzcatalog>
             <class>Xyz_Catalog_Model</class>
        </xyzcatalog>
    </models>
    <events>
      <sales_order_save_after>
        <observers>
          <xyz_catalog_price_observer>
            <class>Xyz_Catalog_Model_Price_Observer</class>
            <method>apply_quantity_order</method>
          </xyz_catalog_price_observer>
        </observers>
      </sales_order_save_after>
      <sales_quote_save_after>
        <observers>
          <xyz_catalog_price_observer>
            <class>Xyz_Catalog_Model_Price_Observer</class>
            <method>apply_quantity_quote</method>
          </xyz_catalog_price_observer>
        </observers>
      </sales_quote_save_after>
    </events>
  </global>
</config>

Conclusion

With this modification, whenever a customer saves an order or quote with the pricequantity attribute set, your observer will automatically recalculate and update the subtotal according to your custom logic. This ensures that the subtotal is always accurate for your specific product configuration.

Remember, modifying Magento’s core functionality should be done judiciously. Always ensure you have a backup of your data before making significant changes. If you’re looking for more advanced customizations or need professional assistance, don’t hesitate to reach out to our team at Laravel Company. We specialize in enterprise-level Magento development and can help you achieve your business goals.

Happy coding!

Catalog Database Extensions Orders

Our network