Magento - 在自定义页面模板上使用 $this->getPriceHtml

发布于 2024-10-11 01:28:49 字数 1278 浏览 2 评论 0原文

我有一个滚动条,显示当前正在销售的一系列产品,我使用以下命令对其进行调用:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悲喜皆因你 2024-10-18 01:28:49

我的同事建议使用这种 Magento 友好的方法在任何地方获取价格 html:

<?php $_product = Mage::getModel('catalog/product')->load($product->getId());
      $productBlock = $this->getLayout()->createBlock('catalog/product_price');
      echo $productBlock->getPriceHtml($_product); ?>

如果您已经在使用加载的产品,那么您不需要第一行,但是我的产品来自一个集合,所以这是必要的。

My colleague recommended using this Magento friendly method to get the price html anywhere:

<?php $_product = Mage::getModel('catalog/product')->load($product->getId());
      $productBlock = $this->getLayout()->createBlock('catalog/product_price');
      echo $productBlock->getPriceHtml($_product); ?>

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.

一个人的旅程 2024-10-18 01:28:49

问题在于 getPriceHtml() 函数是在 Mage_Catalog_Block_Product 块中定义的,而不是在标准 Mage_Core_Block_Template 中定义的。您需要确保您的块扩展了 Product 块,或者您可以通过以下方式在布局中实现这一点:

<block type="catalog/product" name="blockname" template="path/to/template.phtml">

我还没有测试过,但它应该可以工作。

The issue is that getPriceHtml() function is defined in the Mage_Catalog_Block_Product block, rather than the standard Mage_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:

<block type="catalog/product" name="blockname" template="path/to/template.phtml">

I haven't tested that, but it should work.

人生戏 2024-10-18 01:28:49

您也可以尝试以下操作:

<?php echo Mage_Catalog_Block_Product::getPriceHtml($_product, true) ?>

其中 $_product 与产品对象相关。

You could also try this:

<?php echo Mage_Catalog_Block_Product::getPriceHtml($_product, true) ?>

Where $_product relates to the product object.

染火枫林 2024-10-18 01:28:49

要让 getPriceHtml() 函数在自定义块中正常工作,您需要做两件事

1) 创建块类型目录/产品

<block type="catalog/product" name="home_page_product" after="default_home_page" template="custom/home_page_product.phtml"/>

2) 将产品对象传递给 getPriceHtml() 函数

<?php $productObject = Mage::getModel('catalog/product')->load($_product->getId());?>
<?php echo $this->getPriceHtml($productObject, true) ?>

To get getPriceHtml() function work correctly in your custom block you need 2 things

1)Make your block type catalog/product

<block type="catalog/product" name="home_page_product" after="default_home_page" template="custom/home_page_product.phtml"/>

2)Pass the product object to getPriceHtml() function

<?php $productObject = Mage::getModel('catalog/product')->load($_product->getId());?>
<?php echo $this->getPriceHtml($productObject, true) ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文