Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the price in quote while adding product to cart: magento

Tags:

magento

I want to change the product price while add that product to cart.

How its possible let me know...

like image 873
Sankar Subburaj Avatar asked Mar 15 '12 14:03

Sankar Subburaj


People also ask

Why add to cart is not working in Magento 2?

Add to Cart not working from homepage It's actually very simple to fix this issue: Login to your Magento 2 backend. Navigate to System > Cache Management and choose all of the caching options built in your Magento 2. In the dropdown, choose Enable and then click on Submit.

What is quote in Magento?

quote = cart contents in Magento.


2 Answers

The way to do it is add an observer which looks for this event 'sales_quote_add_item':

<events>
    <sales_quote_add_item>
        <observers>
            <priceupdate_observer>
                <type>singleton</type>
                <class>mymodule/observer</class>
                <method>updatePrice</method>
            </priceupdate_observer>
        </observers>
    </sales_quote_add_item>
</events>

The observer should have a method which does something like this:

public function updatePrice($observer) {
    $event = $observer->getEvent();
    $quote_item = $event->getQuoteItem();
    $new_price = <insert logic>
    $quote_item->setOriginalCustomPrice($new_price);
    $quote_item->save();
}
like image 188
Gershon Herczeg Avatar answered Oct 18 '22 20:10

Gershon Herczeg


You can use an observer class to listen to checkout_cart_product_add_after, and use a product’s “Super Mode” to set custom prices against the quote item.

In your /app/code/local/{namespace}/{yourmodule}/etc/config.xml:

<config>
    ...
    <frontend>
        ...
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <unique_event_name>
                        <class>{{modulename}}/observer</class>
                        <method>modifyPrice</method>
                    </unique_event_name>
                </observers>
            </checkout_cart_product_add_after>
        </events>
        ...
    </frontend>
    ...
</config>

And then create an Observer class at /app/code/local/{namespace}/{yourmodule}/Model/Observer.php

    <?php
        class <namespace>_<modulename>_Model_Observer
        {
            public function modifyPrice(Varien_Event_Observer $obs)
            {
                $customPrice = Mage::getSingleton(’core/session’)->getCustomPriceCalcuation(); // Provide you price i have set with session
                $p = $obs->getQuoteItem();
                $p->setCustomPrice($customPrice)->setOriginalCustomPrice($customPrice); 
            }

        }
like image 36
Xman Classical Avatar answered Oct 18 '22 20:10

Xman Classical