I want to change the product price while add that product to cart.
How its possible let me know...
I want to change the product price while add that product to cart.
How its possible let me know...
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();
}
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);
}
}
The issue regarding configurable product has been solved. You simply remove the
$quote_item->save();
and then for the product will not added to the cart twice. But one another very serious issue is remaining in this function. That is, with this function we can update the item price in cart but after added to cart, product price are not changed according the different currency. So this function can not be used for a store having multiple currency.
If anyone find any solution on that issue, please share with us...
Soup to Nuts.
File: /app/etc/modules/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Ajax_ProductAdjust>
<codePool>local</codePool>
<active>true</active>
</Ajax_ProductAdjust>
</modules>
</config>
File: /app/code/local/Ajax/ProductAdjust/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Ajax_ProductAdjust>
<version>1.0.1</version>
</Ajax_ProductAdjust>
</modules>
<global>
<models>
<Ajax_ProductAdjust>
<class>Ajax_ProductAdjust_Model</class>
</Ajax_ProductAdjust>
</models>
<events>
<sales_quote_add_item>
<observers>
<ajax_productadjust_model_observer>
<type>singleton</type>
<class>Ajax_ProductAdjust_Model_Observer</class>
<method>updatePrice</method>
</ajax_productadjust_model_observer>
</observers>
</sales_quote_add_item>
</events>
</global>
</config>
File: /app/code/local/Ajax/ProductAdjust/Model/Observer.php
<?php
//Notes
class Ajax_ProductAdjust_Model_Observer
{
public function _construct()
{
}
public function getNewPrice()
{
//Your new functionality here
//
$newprice = "";
return $newprice;
}
public function updatePrice( Varien_Event_Observer $observer )
{
$event = $observer->getEvent();
$quote_item = $event->getQuoteItem();
$new_price = $this->getNewPrice();
$quote_item->setOriginalCustomPrice($new_price);
$quote_item->save();
}
}
Cheers,
As righty answered by Gershon Herczeg, Jürgen Thelen and Xman Classical. You will need to write a observer of sales_quote_add_item event. When any product is added to cart your observer will be triggered. If the product is Configurable then this event will be fired twice, You will have to something like that to get simple product only.
$item = $observer->getEvent()->getQuoteItem();
$quote = $item->getQuote();
$product = $item->getProduct();
if ($product->getTypeId() != "configurable") {
//Do your thing here
}
The question does not state whether this should be done by adding some logic to the code or not. So since you have answers for developer there is also something that is called cart price rules (in admin panel go to Promotions > Shopping Cart Price Rules) where you can create different rules for making sales and discounts (with or without coupons).
To change product price while adding product to cart, use an observer event.
Follow the steps given below
1. Add an observer in your module config.xml file.
2. Create an observer file in your model
3. add checkout_cart_product_add_after event
File: app/code/local/Namespace/Module/etc/config.xml
eg: app/code/local/MGS/Rileytheme/etc/config.xml
<frontend>
<routers>
<routeurfrontend>
<use>standard</use>
<args>
<module>MGS_Rileytheme</module>
<frontName>rileytheme</frontName>
</args>
</routeurfrontend>
</routers>
<layout>
<updates>
<rileytheme>
<file>rileytheme.xml</file>
</rileytheme>
</updates>
</layout>
<events>
<checkout_cart_product_add_after>
<observers>
<rileytheme>
<class>rileytheme/observer</class>
<method>modifyPrice</method>
</rileytheme>
</observers>
</checkout_cart_product_add_after>
</events></b>
<frontend>
File:app/code/local/MGS/Rileytheme/Model/Observer.php
class MGS_Rileytheme_Model_Observer {
public function modifyPrice(Varien_Event_Observer $observer) {
//$order = Mage::registry('current_order'); For getting current order
//$orderid = $order->getRealOrderId(); For getting orderid
$quote_item = $observer->getQuoteItem();
$payment = 30; //add your custom product price here and do your logic here
$quote_item->setOriginalCustomPrice($payment);
$quote_item->save();
return $this;
}
}