Magento:如何获得应用了目录规则的产品的价格

时间:2012-12-30 18:42:55

标签: magento e-commerce catalog

我正在开发一个脚本(Magento外部,而不是模块),旨在输出所有可用产品,价格和其他一些属性的文本列表。但是,目录价格规则似乎不适用于产品价格。如果我使用以下任何一项:

$_product->getPrice()
$_product->getFinalPrice()

我得到正常价格(没有应用规则)。

如果我使用:

$_product->getSpecialPrice()

我得 null ,除非产品实际上在产品本身中插入了特殊价格(即特殊价格与目录规则无关)。

我也试过

Mage::getModel('catalogrule/rule')->calcProductPriceRule($product,$product->getPrice())

正如Fabian Blechschmidt给出的答案所示,但有趣的是,只有当产品受到任何目录规则的影响时,它才会返回正常价格,否则返回 null

前一段时间在StackOverflowMagento Forums中存在类似的问题,但提供的答案(即插入下面的代码)对我来说并不起作用(返回的价格仍然是相同)。

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

有人知道如何实现这个目标吗?

我正在使用Magento 1.6.2.0。 提前谢谢。

4 个答案:

答案 0 :(得分:20)

感谢您,我找到了一个新网站: http://www.catgento.com/magento-useful-functions-cheatsheet/

他们提到:

Mage::getModel('catalogrule/rule')->calcProductPriceRule($product,$product->getPrice())

HTH

答案 1 :(得分:12)

我发现了这个问题。折扣价格在商店前端显示Ok。问题是我正在为Magento开发一个“外部”脚本(因此不是Magento模块),类似于:

<?php

set_time_limit(0);
ignore_user_abort();
error_reporting(E_ALL^E_NOTICE);
header("Content-Type: text/plain; charset=utf-8");

require_once "app/Mage.php";

// Get default store code
$default_store = Mage::app()->getStore();
...

为了使一切正常工作,似乎必须遵循正确的Magento引导程序,并将所有内容开发为模块。我的脚本非常简单,我认为没有必要编写一个完整的模块。换句话说,Magento中的所有东西都应该是一个模块。

结束语,使用模块方法,所有方法都按预期工作:

$_product->getPrice()
$_product->getFinalPrice()
$_product->getSpecialPrice()

感谢大家的投入。

答案 2 :(得分:11)

这有助于我解决这个问题:http://www.magentocommerce.com/boards/viewthread/176883/ 。 Jernej的解决方案似乎是合理的,但它不处理通过使用“停止处理”覆盖其他规则的规则,因此可以应用多个规则。

$original_price = $_product->getPrice();
$store_id = 1; // Use the default store
$discounted_price = Mage::getResourceModel('catalogrule/rule')->getRulePrice( 
                        Mage::app()->getLocale()->storeTimeStamp($store_id), 
                        Mage::app()->getStore($store_id)->getWebsiteId(), 
                        Mage::getSingleton('customer/session')->getCustomerGroupId(), 
                        $_product->getId());

// if the product isn't discounted then default back to the original price
if ($discounted_price===false) {
    $discounted_price=$original_price;
}

答案 3 :(得分:10)

由于目录价格规则在很大程度上取决于时间,商店和访问客户,因此当您想要使用它的价格规则检索产品最终价格时,您需要设置这些参数。

因此,在您的情况下,请确保提供的产品与所需的商店和客户组ID一起传递,该ID可以设置为:

Mage::getModel('catalogrule/rule')->calcProductPriceRule($product->setStoreId('STORE_ID')->setCustomerGroupId('CUSTOMER_GROUP_ID'),$product->getPrice())
相关问题