2

I am trying to create a new list.phtml for a category that has a bunch of smaller products for our store. I got everything working right, except for the fact that the list.phtml is calling my whole catalog and not the actual category that was clicked on. Anyone know how I can change this, with out setting a category id to the specific list.phtml, so I can continue to use it for other categories as well?

<?php
                // some helpers
                $_helper = $this->helper('catalog/output');
                $storeId = Mage::app()->getStore()->getId();
                $catalog = $this->getLayout()->createBlock('catalog/product_list')->setStoreId($storeId);
                $productCount = $this->getData('num_products');

$visibility = array(
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                      Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                  );
$collection = Mage::getResourceModel('reports/product_collection')
                        ->addAttributeToSelect('*')    
                        ->setStoreId($storeId)
                        ->addStoreFilter($storeId)    
                        ->addAttributeToFilter('visibility', $visibility)             
                        ->setPageSize($productCount);
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);


/* To display product by category id */
  if($categoryId=$this->getData('category_id')){
        $category = Mage::getModel('catalog/category')->load($categoryId);
        $collection->addCategoryFilter($category);
      } ?>

  <div class="inner-div">
        <?php     // if no products are in list, display some text
                 if (!$collection->count()) :
        ?>
        <p class="note-msg"><?php echo $this->__('There are no products at the moment.') ?></p>
        <?php else : ?>

     <?php if($categoryId=$this->getData('category_id')) { 
              $category = Mage::getModel('catalog/category')->load($categoryId);
      ?>
        <h2 class="category-pro-title"><span><?php echo $category->getName() ?></span></h2>
    <?php } else { ?>
        <h2 class="category-pro-title"><span><?php echo $this->__('Category List') ?></span></h2>
    <?php } ?>


<div class="category-products">
  <div class="products small-list">     
        <?php
                $_collectionSize = $collection->count();
                $_columnCount = 4;
                $i = 0;                
                foreach ($collection as $_product) :
                        $_product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($_product->getId());

        ?>
        <?php if ($i++%$_columnCount==0): ?>            
        <?php endif ?>
<div class="item">
   <div class="item-area">
     <div class="product-image-area">

       <a class="product-image" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><img alt="<?php echo $this->htmlEscape($_product->getName()) ?>" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image') ?>"></a>  
     </div> <!--product-image-area-->
     <div class="details-area">
       <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
                  <?php $prod_name = $this->htmlEscape($_product->getName()) ; 
                  if(strlen($prod_name) > 17) {echo substr($prod_name,0,17)."...";}else {echo $prod_name;}?>
             </a>
        </h2>
        <?php
                        $summaryData = Mage::getModel('review/review_summary')
                                ->setStoreId($storeId)
                                ->load($_product->getId());
                        if($summaryData->getRatingSummary()){ ?>
                            <div class="ratings">
                                <div class="rating-box">
                                        <div class="rating" style="width: <?php echo $summaryData->getRatingSummary().'%'; ?>"></div>
                                </div> <!--rating-box-->
                            </div> <!--ratings-->
        <?php } else { ?>  
                            <div class="ratings"><div class="rating-box"><div style="width:0%" class="rating"></div></div></div>
        <?php } ?>
        <?php echo $this->getPriceHtml($_product, true) ?>
     </div> <!--details-area-->                     
   </div> <!--item-area-->
</div> <!--item-->      
            <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>            
            <?php endif ?>

                <?php endforeach ?>
      </div> <!--products small-list-->    
    </div> <!--category-products-->

        <?php endif ?>  <!--Endif !$collection->count() -->
  </div> <!--inner-div-->
1

This oughtta do what you need:

<?php

$categoryid = 12;

$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');

foreach ($collection as $_product) { ?>

<a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /></a> <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>

<?php } ?>
0

Just change if($categoryId=$this->getData('category_id')) with:

$currCategory = Mage::registry('current_category');
if($currCategory && $categoryId=$currCategory->getId())

Unless you are using a widget you need Mage::registry('current_category') to get the current category.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.