Show only current category’s products in Product View

Show only current category’s products in Product View

July 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Show only current category’s products in Product View

Show Only Current Category’s Products in Product View

I understand that you’re struggling to display only the products associated with the current category on your Magento product page. Let’s dive into this problem step-by-step.

The Current Issue

The code you provided is almost there. It successfully retrieves all categories associated with a specific product and then attempts to load those products. However, it doesn’t filter the results based on the “current” category of the page being viewed.

Step 1: Identify the Current Category ID

The first thing we need to do is correctly identify the current category’s ID using Magento’s built-in functionality:

$currentCategory = Mage::getModel('catalog/layer') -> getCurrentCategory();
$currentCategoryId = $currentCategory -> getId();

Step 2: Filter Products by Current Category ID

Next, we need to filter the product collection returned by your initial query. We can do this using the addCategoryFilter method:

->addCategoryFilter($currentCategory)

However, you’re currently filtering for categories at level 4 (child categories of parent categories), which isn’t quite what you want.

Step 3: Modify Your Existing Code

Let’s modify your existing code to correctly filter the products by the current category:

$categoryCollection = $_product->getCategoryCollection();
foreach ($categoryCollection as $category) {
    if ($category->getId() == $currentCategoryId) { // Current Category ID match
        $products = Mage::getResourceModel('catalog/product_collection')
            ->addCategoryFilter($category)
            ->addAttributeToSelect('small_image');
    }
}

Step 4: Display the Filtered Products

Now, you can loop through your $products collection to display only those associated with the current category:

foreach ($products as $product) {
    // Your existing code for displaying product details goes here
}

The Final Code Structure

Here’s how your modified code should look:

<div class="box base-mini mini-related-items">
  <!-- Your existing code -->

  <?php
  $currentCategory = Mage::getModel('catalog/layer')->getCurrentCategory();
  $currentCategoryId = $currentCategory->getId();

  if ($_product) {
      // Get collection of categories this product is associated with
      $categories = $_product->getCategoryCollection()
          ->addFieldToFilter('level', 4)
          ->load();

      foreach ($categories as $_category) {
          if ($_category->getId() == $currentCategoryId) { // Current Category ID match
              $products = Mage::getResourceModel('catalog/product_collection')
                  ->addCategoryFilter($_category)
                  ->addAttributeToSelect('small_image');

              echo '<div class="content"><ol>';
              foreach ($products as $productModel) {
                  $_product = Mage::getModel('catalog/product')->load($productModel->getId());
                  // Your existing code for displaying product details
              }
              echo '</ol></div>';
          }
      }
  }

  ?>
</div>

Conclusion

Filtering products by the current category in Magento is straightforward once you understand how to retrieve the “current” category and apply this filter correctly. This solution ensures that only those products associated with the current category are displayed on your product page.

Remember, when dealing with complex filtering like this, it’s crucial to test thoroughly across different scenarios to ensure everything works as expected.

For further reading and best practices in Magento development, I recommend checking out articles at LaravelCompany. They provide comprehensive guides for developers looking to master the platform.

Catalog Database Errors

Our network