Magento:如何在应用了目录规则的情况下获取产品的价格 [英] Magento: how to get the price of a product with catalog rules applied

查看:33
本文介绍了Magento:如何在应用了目录规则的情况下获取产品的价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing a script (external to Magento, not a module) which aims to output a text list of all available products, their prices and some other attributes. However, catalog price rules don't seem to be applied to product prices. If I use any of the following:

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

我得到的是正常价格(没有应用规则).

I get the normal price (without rules being applied).

如果我使用:

$_product->getSpecialPrice()

我得到 null 除非产品实际上在产品本身中插入了特价(即如果特价与目录规则无关).

I get null unless the product actually has a special price inserted in the product itself (i.e. if special price is not related with catalog rules).

我也试过

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

正如 Fabian Blechschmidt 给出的答案所建议的那样,但有趣的是,它仅在产品受任何目录规则影响时才返回正常价格,否则返回 null.

as suggested in the answer given by Fabian Blechschmidt, but interestingly it returns the normal price only if the product is affected by any catalog rule, returning null otherwise.

StackOverflow 中有一个类似的问题Magento 论坛 前段时间,但提供的答案(这是插入下面的代码)对我不起作用(返回的价格保持不变).

There was a similar question in StackOverflow and Magento Forums some time ago, but the provided answer (which is to insert the code bellow) doesn't work for me (returned prices remain the same).

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

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

Does anybody have an idea of how to achieve this?

我使用的是 Magento 1.6.2.0.提前致谢.

I'm using Magento 1.6.2.0. Thanks in advance.

推荐答案

我发现了这个问题.折扣后的价格在商店前台显示为 Ok.问题是我正在为 Magento 开发一个外部"脚本(因此不是 Magento 模块),例如:

I discovered the problem. The discounted prices display Ok in the store frontend. The problem was that I was developing a script "external" to Magento (thus not a Magento module), something like:

<?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 中的一切都应该是一个模块.

For everything to work properly it seems that one must follow the proper Magento bootstrap, and develop everything as a module. My script was so simple that I thought it wouldn't be necessary to code a complete module. In other words, everything in Magento should really be a module.

最后,使用模块方法,所有方法都按预期工作:

Concluding, using the module approach, all the methods work as expected:

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

感谢大家的投入.

这篇关于Magento:如何在应用了目录规则的情况下获取产品的价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆