【问题标题】:Magento: how to retrieve product final price (taking in account catalog price rules etc) while out of frontendMagento:如何在前端之外检索产品最终价格(考虑目录价格规则等)
【发布时间】:2012-09-05 16:57:02
【问题描述】:
问题:
在后端或 cron 任务中(例如,在根据用户请求或 cron 事件运行的自定义产品提要生成器中),并且通常在前端之外,Mage_Catalog_Model_Product 的方法 getFinalPrice() 返回产品的基本价格,而不考虑,例如,适用于该产品的 Magento 目录价格规则。
这意味着生成的提要中导出的价格与 Magento 前端中显示的价格不同,这很容易成为问题。
那么,如何获取产品在前端显示的最终价格?
【问题讨论】:
标签:
php
api
magento
backend
catalog
【解决方案1】:
如果您将此行添加到顶部,则提出问题的人所述的解决方案仅适用于管理站点:
Mage::app()->loadAreaPart(Mage_Core_Model_App_Area::AREA_FRONTEND,Mage_Core_Model_App_Area::PART_EVENTS);
这会加载考虑到管理站点上定义的目录价格规则的前端事件。
希望它对未来的人有所帮助。
【解决方案2】:
解决方案
使用辅助类,定义如下方法:
class My_Package_Helper_Price extends Mage_Core_Helper_Abstract {
private $_init_rule = false;
private function initRuleData($product) {
if ($this->_init_rule || empty($product)) return $this;
$productStore = Mage::getModel('core/store')->load($product->getStoreId());
if (!empty($productStore)) {
Mage::register('rule_data', new Varien_Object(array(
'store_id' => $product->getStoreId(),
'website_id' => $productStore->getWebsiteId(),
'customer_group_id' => 0, // NOT_LOGGED_IN
)));
$this->_init_rule = true;
}
return $this;
}
public function getProductFinalPrice($product) {
$this->initRuleData($product);
return $product->getFinalPrice();
}
}
然后,你可以使用:
Mage::helper('my_package/price')->getProductFinalPrice($product);
其中 $product 是 Mage_Catalog_Model_Product 类的加载实例。