Magento – Dynamically add extra price on subtotal on cart

cartevent-observermagento-1.9shopping-cart-price-rules

I need your help in solving one of my issue related to adding extra/custom price on subtotal on cart page. I have called a observer for checkout_cart_product_add_after and sales_convert_quote_item_to_order_item event and in which I am trying to add dynamic price from below code

$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);

This is allow me add in unit Price but I want to add in Subtotal of product. Also I have to tried to add price via custom options but it adds in Additional_optional field and not stored in info_buyrequest so might it not reflect to the subtotal price.

Please advise for suggestion.

Thanks

Best Answer

You can accomplished this by overriding Mage_Sales_Model_Quote_Item via modules config.xml:

<global>
    <models>
        <sales>
            <rewrite>                    
            <quote_item>Namespace_Module_Model_Quote_Item</quote_item>
            </rewrite>
        </sales>
    </models>
</global>

And then overriding the calcRowTotal()

class Namespace_Module_Model_Quote_Item extends Mage_Sales_Model_Quote_Item
{
    protected $customRowTotalPrice = null;

    public function setCustomRowTotalPrice($price)
    {
        $this->customRowTotalPrice = $price;
    }

    public function calcRowTotal()
    {
        if ($this->customRowTotalPrice !== null) {
            $this->setRowTotal($this->getStore()->roundPrice($this->customRowTotalPrice));
            $this->setBaseRowTotal($this->getStore()->roundPrice($this->customRowTotalPrice));
            return $this;
        }

        $qty = $this->getTotalQty();
        $total = $this->getStore()->roundPrice($this->getCalculationPriceOriginal()) * $qty;
        $baseTotal = $this->getStore()->roundPrice($this->getBaseCalculationPriceOriginal()) * $qty;

        $this->setRowTotal($this->getStore()->roundPrice($total));
        $this->setBaseRowTotal($this->getStore()->roundPrice($baseTotal));
        return $this;
    }

}