0

问题

在后端 o cron 任务中(例如,在根据用户请求或 cron 事件运行的自定义产品提要生成器中),并且通常在前端之外,Mage_Catalog_Model_Product 的方法 getFinalPrice() 返回产品的基本价格,而不考虑,例如, 适用于该产品的 Magento 目录价格规则。

这意味着生成的提要中导出的价格与 Magento 前端中显示的价格不同,这很容易成为问题。

那么,如何获取产品在前端显示的最终价格呢?

4

2 回答 2

4

如果您将此行添加到顶部,则提出问题的人所述的解决方案仅适用于管理站点:

Mage::app()->loadAreaPart(Mage_Core_Model_App_Area::AREA_FRONTEND,Mage_Core_Model_App_Area::PART_EVENTS);

这会加载考虑到管理站点上定义的目录价格规则的前端事件。

希望它对将来的人有所帮助。

于 2013-01-08T15:00:46.897 回答
1

解决方案

使用辅助类,并定义以下方法:

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 类的加载实例。

于 2012-09-05T16:57:02.010 回答