Reputation: 1
I use the code below for changing the price into the Magento cart dynamic.
class Test_Pricecalc_Model_Observer
{
public function modifyPrice(Varien_Event_Observer $obs)
{
// Get the quote item
$item = $obs->getQuoteItem();
// Ensure we have the parent item, if it has one
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
// Load the custom price
$price = $this->_getPriceByItem($item);
// Set the custom price
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
// Enable super mode on the product.
$item->getProduct()->setIsSuperMode(true);
}
protected function _getPriceByItem(Mage_Sales_Model_Quote_Item $item)
{
$price = $price + 10;
return $price;
}
protected function _getRequest()
{
return Mage::app()->getRequest();
}
}
Does someone now how to get the custom price from the product detail page after choosing the product options there? I want to use that price as basis price fur my further calculation.
Upvotes: 0
Views: 1027
Reputation:
You can create a hidden input field in product detail page with value equal to your custom price. and can get that field in your observer like:
$new_price = Mage::app()->getRequest()->getPost('custom_price');
where 'custom_price' is the name of your hidden field and $new_price in oberserver is your custom price.
here is an example, which we used to render our product price during "add to cart":-
class Tech9_Myprice_Model_Observer
{
public function modifyPrice(Varien_Event_Observer $obs)
{
// Get the quote item
$item = $obs->getQuoteItem();
// Ensure we have the parent item, if it has one
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
// Load the custom price
$new_price = Mage::app()->getRequest()->getPost('custom_price');
Mage::log( $new_price);
$price = $new_price;
// Set the custom price
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
// Enable super mode on the product.
$item->getProduct()->setIsSuperMode(true);
}
}
Here Tech9 is the name of our package and Myprice is the name of our module.
We had to use our custom price during "add to cart" event. We prepared a separate module for it , if you like we can provide you with that package as well.
Upvotes: 1