Search code examples
magentooverwritemagento-1.8subtotal

Change subtotal with given price & quantity (custom attribute)


My price is for example 10,00 € this price is for 100 g the customer can add any g in the quantity field for example 300

magento has now a subtotal of 3000, its ok but not for my needs here.

i need to do: if price & quantity is set, get subtotal / price quantity, set new subtotal

where can i put my modifications for this?

Thank you very much! Dennis

Edit and the third try of observer (not working atm, no error, nothing happens):

    class Xyz_Catalog_Model_Price_Observer
    {
        public function __construct()
        {
        }

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

        $pricequantity = $order->getPricequantity();

        if($pricequantity != ''){
            $old_sub_total = $order->getTotals();
            $new_sub_total = $old_sub_total / 100;
            $order->setTotals($new_sub_total);
        } else {}

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

        $pricequantity = $quote->getPricequantity();

        if($pricequantity != ''){
            $old_sub_total = $quote->getTotals();
            $new_sub_total = $old_sub_total / 100;
            $quote->setTotals($new_sub_total);
        } else {}

        return $this;
        }
    }

XML:

<?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>

Solution

  • Rather than overriding sub-total calculation function, I suggest to try events - sales_quote_save_after and sales_order_save_after.

    You can get quote and sales in observer method by

    $observer->getEvent()->getOrder() //for order
    $observer->getEvent()->getQuote() //for quote
    

    Then modify the subtotal accordingly.

    Edit: It might be just hint how can you modify sub total.

    Edit2: You have to add event observer in your config as shown:

    <sales_order_save_after>
        <observers>
            <yourext>
                <class>yourext/observer</class>
                <method>observerMethod</method>
            </yourext>
        </observers>
    </sales_order_save_after>
    

    For detail, have look on Customize Magento using Event/Observer