簡體   English   中英

我想在Magento的list.phtml中顯示類別及其產品

[英]I want to display categories and its products in list.phtml in Magento

我是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

根據我的理解,我得出以下解決方案:

<?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>標記關聯。

單擊時,將顯示所有相應類別的產品(以列表/網格模式)。

管理類別時的常見錯誤

要管理產品,請檢查此鏈接如何管理產品

顯示產品的原因

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM