我正在尝试以类似于 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/