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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4652808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:52:18  来源:igfitidea点击:

Magento - using $this->getPriceHtml on custom page template

magentophp

提问by Marlon Creative

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 来获取单个产品:

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).

然而,这给了我一个空白。如果我执行 var_dump 我可以看到原始价格和特价都可用,那么为什么这不起作用?我在我的主页模板上使用完全相同的代码,我通过主页 CMS 调用它,并且价格显示正常(划掉正常价格并显示特价)。

Using $_product->getFinalPrice()works fine, but only gives me the final "special" price and doesn't show the original price.

使用$_product->getFinalPrice()工作正常,但只给我最终的“特殊”价格而不显示原始价格。

Am I maybe missing something in my xml layout that's needed to show the prices using getPriceHtml?

我是否可能在我的 xml 布局中遗漏了使用getPriceHtml显示价格所需的某些内容?

回答by Johnatilley

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

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

<?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.

如果您已经在使用已加载的产品,那么您将不需要第一行,但是我的产品来自一个系列,因此这是必要的。

回答by Jonathan Day

The issue is that getPriceHtml()function is defined in the Mage_Catalog_Block_Productblock, 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:

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

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

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

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

回答by Gerard Nijboer

You could also try this:

你也可以试试这个:

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

Where $_productrelates to the product object.

其中$_product涉及的产品的对象。

回答by Suman-PHP4U

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

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

1)Make your block type catalog/product

1)制作您的块类型目录/产品

<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

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

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