1

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>
user715449
  • 113
  • 1
  • 2
  • 12

2 Answers2

1

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

Krishna Sunuwar
  • 2,915
  • 16
  • 24
  • where can i work with this "Events", i am maybe able to get some code to work, i just dont know where the right place for it is. Thx – user715449 Jun 04 '14 at 19:47
  • The exsample in the link worked. My try of my own code is for sure wrong at all :-) (Look my edit). I am unsure with the xml ? And how to use Order and Quote the right way here. Maybe i am close and you can help me once more. Thank you very much. – user715449 Jun 05 '14 at 10:52
  • ops, I am sorry about tax, in fact I copied that code. it should be `yourext` I make edit on my answer, let me edit your question. See KrishEdit1 part. – Krishna Sunuwar Jun 05 '14 at 10:58
  • I am sorry, discard my edit. You have to add two `tax` can be replaced by anything like `ayourext` or anything. It is just id of event observer. You have to added two events `sales_order_save_after` and `sales_quote_save_after`. – Krishna Sunuwar Jun 05 '14 at 11:05
  • i updated my code at the top. i think we get close, but getQuote dont work. Thx – user715449 Jun 05 '14 at 11:59
  • I am sorry, I did another typo mistake, `getEvent()` is function so please do it like `$observer->getEvent()->getQuote()`. Notice (). I edited my answer too. – Krishna Sunuwar Jun 05 '14 at 12:01
  • no error anymore, but my try to change the subtotals failed, just nothing happens. Do i use get und set the right way? Thank you, i learn much about magento again =) – user715449 Jun 05 '14 at 12:08
  • Please debug your `apply_quantity_quote()` and `apply_quantity_order()` functions line by line. There are some mistake such as you have to call `save()` method after you make changes. Such as call `$order->save()` at very last line. – Krishna Sunuwar Jun 05 '14 at 12:11
  • Also, updating sup-total is not that easy, you have to edit lot of other fields. See Mage_Sales_Model_Quote::collectTotals() function for example – Krishna Sunuwar Jun 05 '14 at 12:13
  • I think it is time that you accept my answer. Please click accept icon (right symbol). – Krishna Sunuwar Jun 05 '14 at 12:14
1

Take a look @ Programmatically add product to cart with price change

public function applyDiscount(Varien_Event_Observer $observer)
{
    /* @var $item Mage_Sales_Model_Quote_Item */
    $item = $observer->getQuoteItem();
    if ($item->getParentItem()) {
        $item = $item->getParentItem();
    }

    // calc special price
    $percentDiscount = 5;
    $specialPrice = $item->getOriginalPrice() -  $percentDiscount;

    // Make sure we don't have a negative
    if ($specialPrice > 0) {
        $item->setCustomPrice($specialPrice);
        $item->setOriginalCustomPrice($specialPrice);
        $item->getProduct()->setIsSuperMode(true);
    }
}
Community
  • 1
  • 1
MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62