Magento - 在自定义页面模板上使用 $this->getPriceHtml
我有一个滚动条,显示当前正在销售的一系列产品,我使用以下命令对其进行调用:
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', $visibility)
->setPageSize(4) // Only return 4 products
->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('special_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToSort('special_from_date', 'desc');
$_productCollection->load();
然后我运行 foreach 来获取各个产品:
foreach ($_productCollection as $_product)
一切正常,除了价格,我通常会使用它来调用
$this->getPriceHtml($_product, true)
但是这是给出的我一片空白。如果我执行 var_dump,我可以看到原价和特价都可用,那么为什么这不起作用呢?我在主页模板上使用完全相同的代码,通过主页 CMS 调用该代码,并且价格显示正常(常规价格划掉并显示特价)。
使用 $_product->getFinalPrice() 效果很好,但只给我最终的“特殊”价格,而不显示原始价格。
我是否可能在 xml 布局中遗漏了使用 getPriceHtml 显示价格所需的内容?
I have a scroller showing a collection of products currently on sale, which I call using the following:
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', $visibility)
->setPageSize(4) // Only return 4 products
->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('special_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToSort('special_from_date', 'desc');
$_productCollection->load();
I then run a foreach to get the individual products:
foreach ($_productCollection as $_product)
Everything works fine, except for the price, which I would usually call using
$this->getPriceHtml($_product, true)
However this is giving me a blank. If I do a var_dump I can see that both the original price and the special price are both available, so why isn't this working? I use exactly the same code on my homepage template, which I call through the homepage CMS, and the price is shown fine (with the regular price crossed out and special price shown).
Using $_product->getFinalPrice() works fine, but only gives me the final "special" price and doesn't show the original price.
Am I maybe missing something in my xml layout that's needed to show the prices using getPriceHtml?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的同事建议使用这种 Magento 友好的方法在任何地方获取价格 html:
如果您已经在使用加载的产品,那么您不需要第一行,但是我的产品来自一个集合,所以这是必要的。
My colleague recommended using this Magento friendly method to get the price html anywhere:
If you're already working with a loaded product then you won't need the first line, however my product was from a collection so this was necessary.
问题在于
getPriceHtml()
函数是在Mage_Catalog_Block_Product
块中定义的,而不是在标准Mage_Core_Block_Template
中定义的。您需要确保您的块扩展了 Product 块,或者您可以通过以下方式在布局中实现这一点:我还没有测试过,但它应该可以工作。
The issue is that
getPriceHtml()
function is defined in theMage_Catalog_Block_Product
block, rather than the standardMage_Core_Block_Template
. You need to ensure that your block extends the Product block, or you can achieve that in your layout by something like:I haven't tested that, but it should work.
您也可以尝试以下操作:
其中
$_product
与产品对象相关。You could also try this:
Where
$_product
relates to the product object.要让 getPriceHtml() 函数在自定义块中正常工作,您需要做两件事
1) 创建块类型目录/产品
2) 将产品对象传递给 getPriceHtml() 函数
To get getPriceHtml() function work correctly in your custom block you need 2 things
1)Make your block type catalog/product
2)Pass the product object to getPriceHtml() function