php - 无法从 Magento 产品集合中检索价格信息

标签 php magento magento-1.7

我正在尝试以类似于 list.phtml 网格格式的方式在任意页面上输出某个类别的产品。

我有以下代码片段:

$category = Mage::getModel('catalog/category');
$category->load(17);
$_productCollection = $category->getProductCollection()
                               ->addAttributeToSelect('name');
$_helper = Mage::helper('catalog/output');

这给了我一个产品集合,然后我对其进行迭代:

foreach ($_productCollection as $_product): 

<!-- This works -->
<h2 class="product-name">
    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">
        <?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
    </a>
</h2>

<!-- This does not -->
<?php echo $this->getPriceHtml($_product, true) ?>

<!-- This just returns out of stock -->
<div class="actions">
    <?php if($_product->isSaleable()): ?>
        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')">
            <span><span><?php echo $this->__('Add to Cart') ?></span></span>
        </button>
    <?php else: ?>
        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>                                                    
</div>

endforeach;

上面的代码除了顶部获取产品集合的调用之外,都是从list.phtml借用的。

谁能告诉我为什么价格和可售信息不可用,因此为什么该商品出现缺货?以前,当产品名称不可用时,我必须添加 ->addAttributeToSelect('name'),我需要添加一些类似的内容吗?

最佳答案

请在您的 phtml 文件中尝试以下代码。

$category = Mage::getModel('catalog/category')->load(3);
$_productCollection = $category->getProductCollection()->addAttributeToSelect('*'); 
$productBlock=$this->getLayout()->createBlock("catalog/product");
foreach($_productCollection  as $_product)
{
    //for get the price of product 
    if($_product->isSaleable()) //this will check if product is in stock
       echo $productBlock->getPriceHtml($_product,true);
}

关于php - 无法从 Magento 产品集合中检索价格信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12618322/

相关文章:

php - ServiceStack JsonSerializer.DeserializeFromString 不适用于 UTF-8 字符串

php - Cakephp 3 newEntity() 不工作

magento - 面包屑未显示在 magento cms 页面中

python - Magento SOAP v1 过滤器不过滤(Python Suds)

css - 在所有扩展 css 文件之后添加 css 文件

php - 通过 PHP 中的 SendGrid 以编程方式发送电子邮件(批量电子邮件)

php - 将 PHP 变量设置为 MySQL 通配符 '%'

php - 我可以在 quote->getAllItems() 上调用哪些方法?

php - 过滤两个类别的产品集合 Magento 1.7

Magento 重新索引数据 - 风险