2

我是 Magento 的新手,我需要将类别显示为标题并列出该类别下的所有产品。不知何故,我在搜索网络时得到了一些相关代码,但它不起作用。可以将类别显示为标题,但不能显示其对应的产品。

我将提供用于显示类别和产品的代码。

<?php 

$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();

$tree->load();
$ids = $tree->getCollection()->getAllIds();
$arr = array();

if ($ids){
    foreach ($ids as $id){
        $cat = Mage::getModel('catalog/category');
        $cat->load($id);
        if ($cat->getIsActive()) {
            $arr[$id] = $cat->getName();
            $arr[$catId] =  $cat->getId();


?>

<div class="right">
                    <div class="products">
                    <h2 class="head"><?php echo $arr[$id]; ?></h2>
<?php 
$catagory_model = Mage::getModel('catalog/category')->load($id); //where $category_id is the id of the category

$collection = Mage::getResourceModel('catalog/product_collection');

$collection->addCategoryFilter($catagory_model); //category filter

$collection->addAttributeToFilter('status',1); //only enabled product

$collection->addAttributeToSelect(array('name','url','small_image')); //add product attribute to be fetched

//$collection->getSelect()->order('rand()'); //uncomment to get products in random order

$collection->addStoreFilter();

if(!empty($collection))

{

    foreach ($collection as $_product):


    ?>                      
                    <div class="pro-list">

  <h5><?php echo $_product->getname(); ?></h5>
 <!-- other product details -->
  </div>
 <?php 
 endforeach;

 }else

 {

    echo 'No products exists';

 }


 ?> 
</div>
</div>
<?php }
        }
        }
        ?> 

我做错了什么?代码是用template/catalog/product/list.phtml.

4

1 回答 1

0

据我了解,我得出了这个解决方案:

<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
    <div id="navigation_vert">
        <?php if (count($categories) > 0): ?>
            <ul>
                <?php foreach($categories as $category): ?>
                    <li>
                        <a class="link" href="<?php echo $helper->getCategoryUrl($category) ?>">
                        <?php echo $this->escapeHtml($category->getName()) ?>
                        </a>
                        <?php $subcategories = $category->getChildren() ?>
                        <?php if (count($subcategories) > 0): ?>
                            <?php foreach($subcategories as $subcategory): ?>
                                <a href="<?php echo $helper->getCategoryUrl($subcategory) ?>">
                                <?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?>
                                </a>
                                    <?php $subsubcategories = $subcategory->getChildren() ?>
                                    <?php if (count($subsubcategories) > 0): ?>
                                        <ul>
                                            <?php foreach($subsubcategories as $subsubcategory): ?>
                                                    <li>
                                                        <a href="<?php echo $helper->getCategoryUrl($subsubcategory) ?>"><?php echo $this->escapeHtml(trim($subsubcategory->getName(), '- ')) ?></a>
                                                    </li>
                                            <?php endforeach; ?>
                                        </ul>
                                    <?php endif; ?><!-- subsubcategories if e -->
                            <?php endforeach; ?><!-- subcategories for e -->
                        <?php endif; ?><!-- subcategories if e -->
                    </li>
                <?php endforeach; ?><!-- categories e -->
            </ul>
        <?php endif; ?><!-- count main categories if e -->
    </div><!-- id navigation_vert e -->


在上面的代码中,我们提取了所有类别(其子类别--->及其子子类别(仅供参考))
每个类别都与<a>标签相关联。

单击 后,将显示所有相应类别的产品(以列表/网格模式)。管理类别时的

常见错误。 要管理产品,请查看此链接如何管理产品不显示产品 的原因。



于 2012-10-26T06:00:47.923 回答