magento - 如何在主页中仅显示一类产品

时间:2014-11-28 23:17:28

标签: php magento web

我在主页上查看产品时遇到问题。 更准确地说,如果我使用此代码:

$_productCollection=$this->getLoadedProductCollection();    

我收到消息“没有符合选择的产品。”

但如果我用以下代码替换上面的代码:

$_productCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('visibility', 4)->addAttributeToFilter('status', 1)->load();    

然后我显示了目录中的所有产品,即使我在主页上指定了这样的类别,也可以看到更多:

{{block type="catalog/product_list" category_id="24" template="catalog/product/list-home.phtml"}}    

我该如何解决?

P.S。当我从导航栏中选择产品类别时,我遇到了同样的问题。

提前感谢您的帮助。

****编辑****

我在CMS主页中使用的

代码是:

{{block type="catalog/product_list" category_id="24" template="catalog/product/list-home.phtml"}}    

使用文件list-home.phtml的代码是:

<?php    
$_productCollection=$this->getLoadedProductCollection();    
$_helper = $this->helper('catalog/output');?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount();?>
<ul class="products-grid row">
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php /* if ($i++%$_columnCount==0): ?>
<ul class="products-grid row">
<?php endif */ ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first
<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>  col-sm-4 col-xs-6"> 
<div class="item-container img-thumbnail">
<div class="item-flipper">
<div class="item-front">
<!-- front content -->
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(250,310); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="img-responsive" /></a>
</div>
<div class="item-back">
<!-- back content -->
<div class="book-info">
<div class="h4"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></div>
<div class="short-description"><?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?></div>
</div>
<p><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>" class="btn btn-block btn-success"><?php echo $this->__('Book Details')></a></p>
</div>
</div>
</div>
<h3 class="panel-title 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></h3>
<div class="pull-left"> <?php echo $this->getPriceHtml($_product, true) ?> </div>
<div class="pull-right">
<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="btn btn-warning btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><?php echo $this->__('Add to Cart') ?></button>
<?php else: ?>
<p class="btn btn-alert availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
</li>
<?php if ($i==6) break; ?>
<?php /*  if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif */ ?>
<?php endforeach ?></ul>
</div>
<?php endif; ?>    

用上面的代码我不能看到任何产品。 如果我替换我在开头说的代码,那么我可以看到所有产品,但我想只显示那些属于我在主页代码中指定的类别。

我无法理解我错在哪里。

3 个答案:

答案 0 :(得分:0)

您可以使用此代码

{{block type="catalog/product_list" name="catalog.product.list" alias="products_homepage" category_id="5" template="catalog/product/list.phtml"}}

答案 1 :(得分:0)

我们可以通过转到Admin panel > CMS > Home轻松设置,并在其中添加以下代码。

{{block type="catalog/product_list" category_id="1" template="catalog/product/list.phtml"}}

其中category_id = 1是您要在主页上显示的类别。您可以在Magento的管理员中从类别页面获取类别ID。

希望这会有所帮助。

答案 2 :(得分:0)

尝试将此类别页面设为主页.., 转到Admin System->Configuration->Web->Default pages(tab)->Default web url  在该字段中,将要在主页中显示的页面的类别ID设为catalog/category/view/**id**