Magento Foreach子类别获得产品列表

时间:2014-03-01 17:59:21

标签: html css magento php

我将首先解释我的情况。

目前,我创建了一个块,其中显示了一个主要类别的所有类别。我使用主要类别的id得到它的子类别。

结果:
This is the list of my categories

“所有产品”是指向当前类别的链接 在“所有产品”项目下面,我需要显示当前类别的所有产品 我目前的进展:

<div class="block block-category">
    <div class="inside-box">
    <div class="block-title block-category-title"><h2><?php echo $this->__('Product List') ?></h2></div>
        <div class="block-category-navigation">
            <ul id="category-nav"> 
            <?php 
            $root = Mage::getModel('catalog/category')->load(5); 
            $subCat = explode(',',$root->getChildren()); 

            $collection  = $root
                         ->getCollection()
                         ->addAttributeToSelect("*")
                         ->addFieldToFilter("entity_id", array("in", $subCat) );

                    foreach($collection as $catname){ ?>
                        <li class="clickToSeeProducts">
                            <a>
                                <?php echo $catname->getName(); ?>
                            </a>
                        </li>  
                        <div class="toggleDiv"><a class="allProducts" href="<?php echo $catname->getURL(); ?>"><?php echo "All Products"; ?></li></a></div>
                <?php } ?>
            </ul>
        </div> 
    </div> 
</div>

知道如何使用我的方法显示我的产品吗? 谢谢

1 个答案:

答案 0 :(得分:0)

试试这个:(未经过测试source

<div class="block block-category">
    <div class="inside-box">
    <div class="block-title block-category-title"><h2><?php echo $this->__('Product List') ?></h2></div>
        <div class="block-category-navigation">
            <ul id="category-nav"> 
            <?php 
                $root = Mage::getModel('catalog/category')->load(5); 
                foreach ($root->getChildren() as $subCat) {
                    $collection = Mage::getModel('catalog/product')
                                     ->getCollection()
                                     ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
                                     ->addAttributeToSelect('*')
                                     ->addAttributeToFilter('category_id', array('in' => $categoryIds))
                    foreach($collection as $catname){ ?>
                        <li class="clickToSeeProducts">
                            <a>
                                <?php echo $catname->getName(); ?>
                            </a>
                        </li>  
                        <div class="toggleDiv"><a class="allProducts" href="<?php echo $catname->getURL(); ?>"><?php echo "All Products"; ?></li></a></div>
                    <?php } 
                }
            ?>
            </ul>
        </div> 
    </div> 
</div>