Limit products found by visibility?

Limit products found by visibility?

August 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Limit products found by visibility?

Limiting Products by Visibility in Magento

If you’re using Magento to generate product feeds for channels like Google Shopping, you might find that your scripts include products that are set to ‘Not Visible Individually’. This can happen if the script simply retrieves all products without properly filtering them based on their visibility settings. In this blog post, we’ll explore how to modify your script to only include products with ‘Search’, ‘Catalog’, or ‘Catalog, Search’ visibility.

Understanding Product Visibility in Magento

In Magento, each product has a visibility attribute that determines where the product is displayed:

  • Not Visible Individually: The product won’t be visible on any category page.
  • Catalog: The product will only be visible in search results and catalog pages.
  • Search: The product will only be visible in search results.
  • Catalog, Search: The product will be visible both in search results and catalog pages.

The number 4 you mentioned ($products->addAttributeToFilter('visibility', 4)) actually corresponds to the ‘Not Visible Individually’ setting. This is because Magento assigns an integer value to each visibility option:

  • Not Visible Individually: 1
  • Catalog: 2
  • Search: 3
  • Catalog, Search: 4

Filtering Products by Visibility

To filter your products based on the desired visibility settings, you need to modify the collection query. Instead of filtering for a single visibility value (like 4), you should use an IN operator to include multiple values:

$products->addAttributeToFilter('visibility', array('in' => array(2, 3, 4)));

This line tells Magento to only include products whose visibility attribute is either ‘Catalog’, ‘Search’, or ‘Catalog, Search’.

Refactoring Your Script

Let’s refactor your script with this new filter in place:

<?php
define('SAVE_FEED_LOCATION','google_base_feed.txt');
set_time_limit(1800);
require_once '../app/Mage.php';
Mage::app('default');

try{
    $handle = fopen(SAVE_FEED_LOCATION, 'w');

    // ... (your existing code)

    // Modify the filter here
    $products->addAttributeToFilter('status', 1);
    $products->addAttributeToFilter('visibility', array('in' => array(2, 3, 4)));
    $products->addAttributeToSelect('*');
    $prodIds = $products->getAllIds();

    // ... (your existing code)
}
catch(Exception $e){
    die($e->getMessage());
}

Remember to replace SAVE_FEED_LOCATION with the actual path where you want to save your feed file.

Conclusion

In this post, we’ve seen how to limit the products included in a Magento product feed by filtering them based on their visibility settings. By using an IN operator with a list of desired visibility values (2, 3, and/or 4), you can ensure that only ‘Search’, ‘Catalog’, or ‘Catalog, Search’ products are included.

This approach is more efficient than loading all products and then filtering them in your loop, as it reduces the number of database queries executed. It’s a best practice to always filter collections at the earliest possible stage.

If you’re looking for more advanced Magento scripting techniques or need help with other aspects of e-commerce development, feel free to check out our Magento tutorials and guides on LaravelCompany.

Catalog Configuration

Our network