如何在添加到 magento 购物车之前更改价格?
How to Change the price before adding to cart in magento?
如何在添加到购物车 Magento 时更改产品价格,示例:
suppose I am trying to add 1 product of price , I want
to show the price with *5 = , when I add 2 products I want to
show *10 = 0. so its common multiplier is 5.(multiples of x).
在 app/community/Custom_Module/Modulename/Model 中创建 Observer.php 文件,将以下代码复制到该文件中。
class Custom_Module_Modulename_Model_Observer
{
public function _construct()
{
}
public function getNewPrice()
{
$login = Mage::getSingleton('customer/session')->isLoggedIn();
$roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$userrole = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');
$userrole = strtolower($userrole);
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
$productId = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
}
$batch_qty = $product->getBatchQty();
$actualPrice = $product->getPrice();
$specialPrice = $product->getFinalPrice();
if (isset($batch_qty) && $userrole=="retailer") {
if (isset($specialPrice)) {
$newprice = $specialPrice*$batch_qty;
} else {
$newprice = $actualPrice*$batch_qty;
}
} else {
$newprice= $actualPrice;
}
return $newprice;
}
public function updatePrice($observer)
{
$event = $observer->getEvent();
$product = $event->getProduct();
$quote_item = $event->getQuoteItem();
$new_price = $this->getNewPrice();
$quote_item->setOriginalCustomPrice($new_price);
//$quote_item->save();
$quote_item->getQuote()->save();
//Mage::getSingleton('checkout/cart')->save();
}
}
复制以下代码并将其粘贴到您的 app/community/Custom_Module/Modulename/etc/config.xml 标签内
> <events>
> <sales_quote_add_item>
> <observers>
> <Custom_Module_Modulename_model_observer>
> <type>singleton</type>
> <class>Custom_Module_Modulename_Model_Observer</class>
> <method>updatePrice</method>
> </Custom_Module_Modulename_model_observer>
> </observers>
> </sales_quote_add_item>
> </events>
这对我来说很好。
如何在添加到购物车 Magento 时更改产品价格,示例:
suppose I am trying to add 1 product of price , I want to show the price with *5 = , when I add 2 products I want to show *10 = 0. so its common multiplier is 5.(multiples of x).
在 app/community/Custom_Module/Modulename/Model 中创建 Observer.php 文件,将以下代码复制到该文件中。
class Custom_Module_Modulename_Model_Observer
{
public function _construct()
{
}
public function getNewPrice()
{
$login = Mage::getSingleton('customer/session')->isLoggedIn();
$roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$userrole = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');
$userrole = strtolower($userrole);
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
$productId = $item->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
}
$batch_qty = $product->getBatchQty();
$actualPrice = $product->getPrice();
$specialPrice = $product->getFinalPrice();
if (isset($batch_qty) && $userrole=="retailer") {
if (isset($specialPrice)) {
$newprice = $specialPrice*$batch_qty;
} else {
$newprice = $actualPrice*$batch_qty;
}
} else {
$newprice= $actualPrice;
}
return $newprice;
}
public function updatePrice($observer)
{
$event = $observer->getEvent();
$product = $event->getProduct();
$quote_item = $event->getQuoteItem();
$new_price = $this->getNewPrice();
$quote_item->setOriginalCustomPrice($new_price);
//$quote_item->save();
$quote_item->getQuote()->save();
//Mage::getSingleton('checkout/cart')->save();
}
}
复制以下代码并将其粘贴到您的 app/community/Custom_Module/Modulename/etc/config.xml 标签内
> <events>
> <sales_quote_add_item>
> <observers>
> <Custom_Module_Modulename_model_observer>
> <type>singleton</type>
> <class>Custom_Module_Modulename_Model_Observer</class>
> <method>updatePrice</method>
> </Custom_Module_Modulename_model_observer>
> </observers>
> </sales_quote_add_item>
> </events>
这对我来说很好。