在模板 (phtml) 文件中添加条件会给您带来问题。此价格将显示给访问者,但实际价格字段将用于计算/购物车等,因此可能存在不匹配。
做你想做的最简单的方法可能是覆盖产品模型和/或产品价格模型。
Mage_Catalog_Model_Product
Mage_Catalog_Model_Product_Type_Price
有很多关于如何覆盖模型的指南:
http://magento4u.wordpress.com/tag/override-catalog-product-model/
为简单起见,您可以将类复制到本地命名空间中,这将覆盖该类,而无需创建模块/xml 等。
class Mage_Catalog_Model_Product_Type_Price
{
/**
* Default action to get price of product
*
* @return decimal
*/
public function getPrice($product)
{
if($product->getMsrp() > $product->getData('price')) {
return $product->getMsrp();
}
return $product->getData('price');
}
}
类似的东西可能会奏效,尽管它没有经过任何形状或形式的测试,但它可能会帮助你开始..
或覆盖产品类中的 getPrice 方法
Mage_Catalog_Model_Product
/**
* Get product price throught type instance
*
* @return unknown
*/
public function getPrice()
{
if ($this->_calculatePrice || !$this->getData('price')) {
$price = $this->getPriceModel()->getPrice($this);
} else {
$price = $this->getData('price');
}
if($this->getMsrp() > $price) {
return $this->getMsrp();
}
return $price;
}
您可以看到有时它可能没有使用价格模型,因此在产品类中覆盖 getPrice 方法可能会更好。尝试一下。