0

Our category page child category links are generated using the code below. However, some categories only contain one product. If the category contains only one product is there a way to link directly to that product page rather than the category product listing page?

For example; the 'widgets' category contains only 1 product 'widget'. So rather than the code below generating a link to 'mywebsite.com/main-category/widgets.html' the link would go directly to 'mywebsite.com/widget-product.html

<?php 
                //If there are sub categories
                $categories = $this->getCurrentChildCategories();
                $categoriescount = $this->getCurrentChildCategories()->count();
                if ($categoriescount > 0): 
                ?>
                <div class="container_12">  
                    <?php 
                    //Loop through categories
                    foreach ($categories as $category):
                    ?>
                    <div class="grid_3 alpha special-spacing" style="margin-bottom:10px;">
                        <div>
                        <a href="<?php echo $this->getCategoryUrl($category)?>" class="cat-link"><?php echo $category->getName()?></a>
                        </div>
                        <a href="<?php echo $this->getCategoryUrl($category)?>" class="cat-image">
                        <?php 
                        // If there is a thumbnail set for the category - Display it
                        if($imgUrl = Mage::getModel('catalog/category')->load($category->getId())->getImage()):?>
                        <img src="<?php echo $this->getBaseUrl()."media/catalog/category/".$imgUrl ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
                        <?php endif; ?>
                        </a>
                    </div>

                    <?php endforeach; ?>
                <div class="clear"></div>
                </div>
                <?php else:?>
                <p>No Sub Categories</p>
<?php endif; ?>

2 Answers 2

1

Try this way:

<?php 
            //If there are sub categories
            $categories = $this->getCurrentChildCategories();
            $categoriescount = $this->getCurrentChildCategories()->count();
            if ($categoriescount > 0):  ?>
            <div class="container_12">  
            <?php 
                //Loop through categories
                foreach ($categories as $category): ?>
                <div class="grid_3 alpha special-spacing" style="margin-bottom:10px;">
                    <div>
                    <?php //get category products
                        $categoryProducts = $category->getProductCollection();
                       if(count($categoryProducts) > 1):?>
                         <a href="<?php echo $this->getCategoryUrl($category)?>" class="cat-link"><?php echo $category->getName()?></a>
                      <?php else:?>
                      <?php $product = $categoryProducts->getFirstItem();?>
                        <a href="<?php echo $product->getProductUrl()" class="cat-link"><?php echo $product->getName()?></a>
                      <?php endif;?>
                    </div>
                    <a href="<?php echo $this->getCategoryUrl($category)?>" class="cat-image">
                    <?php 
                    // If there is a thumbnail set for the category - Display it
                    if($imgUrl = Mage::getModel('catalog/category')->load($category->getId())->getImage()):?>
                    <img src="<?php echo $this->getBaseUrl()."media/catalog/category/".$imgUrl ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
                    <?php endif; ?>
                    </a>
                </div>

                <?php endforeach; ?>
            <div class="clear"></div>
            </div>
            <?php else:?>
            <p>No Sub Categories</p>
<?php endif; ?>

We are utilising this method from class Mage_Catalog_Model_Category

  /**
   * Get category products collection
   *
   * @return Varien_Data_Collection_Db
   */
  public function getProductCollection()
  {
      $collection = Mage::getResourceModel('catalog/product_collection')
          ->setStoreId($this->getStoreId())
          ->addCategoryFilter($this);
      return $collection;
  }

Hope this helps.

3
  • Thank you Adarsh - can I ask please is the code above correct? I've added it to the phtml file but it looks as though the php tags don't open/close correctly. Apr 29, 2016 at 9:45
  • I've managed to get it working Adarash but there is no link text - is there anyway I can call the parent category name as the link text rather than the product name? Apr 30, 2016 at 11:38
  • Yea you can, just replace <?php echo $product->getName()?> with any name you want. Apr 30, 2016 at 12:08
0

Instead of coding you can add apache/nginx 301 redirects to url. Let me know if you need syntax.

3
  • Yesha, how does that work? I am looking for some code that says 'if category contains 1 product then product url else category url. Apr 27, 2016 at 18:18
  • I haven't updated code. I had same scenario but I'm using 301 nginx redirects which redirecting category page url to product page url.
    – Yeshu
    Apr 27, 2016 at 18:20
  • We have 1000's of categories however and I am not sure exactly which categories only contain one product. Apr 27, 2016 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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