繁体   English   中英

Magento按类别显示产品

[英]Magento Show products by category

我在产品页面上的list_single.phtml上有此代码。 该代码显示了类别ID为100的15种产品。但是我想显示该产品类别的产品。 当我将-> addCategoryFilter($ _ category)更改为-> addCategoryFilter(4)时,它不起作用。 有什么问题?

 <div class="products_single newProductsContainer container"> <?php $_helper = $this->helper('catalog/output'); $_category = Mage::getModel('catalog/category')->load(100); $_productCollection = Mage::getResourceModel('reports/product_collection') ->addAttributeToSelect('*') ->addCategoryFilter($_category) ->setVisibility(array(2,3,4)); $_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()')); $_productCollection->setPage(1, 15); $i=0; $u=0; foreach ($_productCollection as $_product): $u++; if ($u % 15 == 1) {echo '<div class="row productsrow">';} ?> <div class="item col-md-3 col-xs-12 col-sm-6"> <?php $dynamicproductload = " "; ?> <a class="product-image visible-xs visible-sm" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"> <img id="product-collection-image-<?php echo $_product->getId(); ?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(210,150); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /> </a> <a class="product-image visible-md visible-lg" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"> <img id="product-collection-image-<?php echo $_product->getId(); ?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(210,150); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /> </a> <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> <?php echo $this->getPriceHtml($_product, true) ?> <?php if ($this->getChild('name.after')) { $_nameAfterChildren = $this->getChild('name.after')->getSortedChildren(); foreach ($_nameAfterChildren as $_nameAfterChildName) { $_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName); $_nameAfterChild->setProduct($_product); echo $_nameAfterChild->toHtml(); } } ?> </div> <?php $i++; if ($i % 15 == 0) { echo "</div>"; } ?> <?php endforeach; ?> </div> </div> <?php else: ?> <?php endif; ?> </div> </div> 

如此一来,我很清楚您试图加载产品类别,然后显示与该类别相关的产品吗?

请记住,产品可以是关系中许多类别的一部分,因此它将返回一个数组。 传递产品加载的类别时,从var_dump()得到什么?

$categories = $product->getCategoryIds();
foreach ($categories as $category_id) {
    $_category = Mage::getModel('catalog/category')->load(category_id);
    $_productCollection = Mage::getResourceModel('reports/product_collection')
                   ->addAttributeToSelect('*')
                   ->addCategoryFilter($_category)
                   ->setVisibility(array(2,3,4));

      // Now do enter code here what you need to do with this object
      foreach ($_productCollection as $_product){
        // Do your logic here
      }
} 

尝试使用目录/产品型号

    $_productCollection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToSelect('*')
->addCategoryFilter($_category)->setVisibility(array(2,3,4));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM