I am working for updating the cart price if customer belongs to particular group. If customer is already logged in and add product to cart,cart price is same as i have added for that customer group but if I first add the products in cart and then login the account,cart price do not get updated. How can I update the cart on customer login?
1
votes
1 Answers
0
votes
You need to set an observer that fires on customer_register_success :
<customer_register_success>
<observers>
<yourcompany_yourmodule>
<type>model</type>
<class>yourmodule/observer</class>
<method>modifyItemFinalPrice</method>
</yourcompany_yourmodule>
</observers>
</customer_register_success>
An idea of what can be in the code :
/**
* appelé lors de l'ajout au panier et à chaque fois que le panier est revalidé
* @param Varien_Event_Observer $observer
*/
public function modifyItemFinalPrice(Varien_Event_Observer $observer){
$item = $observer->getData('quote_item');
$this->_manageItem($item);
}
/**
*
* @param Mage_Sales_Model_Quote_Item $item
*/
public function _manageItem($item) {
try {
$product = $item->getProduct();
$newPrice = $this->_calculatePrice($product);
if ($newPrice){
$item->setOriginalCustomPrice($newPrice);
}
} catch (Exception $e){
echo $e->getTraceAsString();
}
}
/**
*
* Retourne un prix en fonction des différentes contraintes du site
*
* @param Mage_Catalog_Model_Product $product
* @return float|boolean
*/
private function _calculatePrice(&$product){
$website_id = Mage::app()->getWebsite()->getId();
$store_id = Mage::app()->getStore()->getId();
$customer = Mage::getModel('customer/session');
$groupeid = $customer->getCustomerGroupId();
Your logic here...
}
I use a function _calculatePrice to calculate the price and for _manageitem as i need it somewhere else but everything can be done in a single function. It works good for me. You can even just get the price following the customer.