Magento Get Current Sort & Direction with getCollection()

Magento Get Current Sort & Direction with getCollection()

July 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Get Current Sort & Direction with getCollection()

Troubleshooting Magento Product Sorting and Simple Product Visibility

In this blog post, we’ll dive into a common issue faced by many Magento developers - sorting products on the catalog page and managing simple product visibility.

The Problem: Incorrect Product Sorting

You’re trying to sort your product collection based on the current order and direction specified in your template file. However, the sorting isn’t working as expected. Let’s troubleshoot this issue step-by-step.

Incorrect Code Snippet Analysis

Here’s what’s happening with the provided code snippet:

  1. You’re creating a new product collection using Mage::getModel('catalog/product')->getCollection().

  2. You’re adding all attributes to select and loading them into memory with addAttributeToSelect('*')->load().

  3. The product collection is being filtered for the current store.

  4. Pagination settings are applied based on the current page and limit specified in your pager block.

  5. Here’s where the issue lies:

    • setOrder($this->getCurrentOrder(), $this->getCurrentDirection())
    • This line assumes that getCurrentOrder() and getCurrentDirection() methods exist in your context.
    • Without providing any logic for these methods, they’ll return null or default values, leading to no sorting being applied.

To resolve this:

  1. Check the Context: Ensure you’re using a block class with correctly defined getCurrentOrder() and getCurrentDirection() methods that fetch data from session variables or URL parameters.

  2. Use Proper Sorting Methods: Consider using built-in Magento functions like $collection->setOrder($field, $direction) instead of relying on custom methods.

Example:

$_productCollection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addStoreFilter()
    ->setPage(Mage::getBlockSingleton('page/html_pager')->getCurrentPage(), $limit)
    ->setPageSize($limit)
    ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_INCATALOG);

$sort = 'position';
if ($this->getRequest()->getParam('order') == 'price') {
    $sort = 'price';
} elseif ($this->getRequest()->getParam('order') == 'name') {
    $sort = 'name';
}

$_productCollection
    ->setOrder($sort, 'asc')
    ->load();

Handling Simple Product Visibility

You mentioned that simple products set to not show individually are still appearing. This is likely due to missing filters in your collection setup.

Solving the Visibility Issue

To ensure only visible products are displayed:

  1. Use the ‘visibility’ attribute filter: Add a field filter for visibility to include only catalog-visible products.

     ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_INCATALOG)
    
  2. Set the correct filters: Make sure all relevant filters (like status, category, etc.) are applied based on your catalog configuration.

Conclusion

In this post, we explored two common Magento development challenges and provided practical solutions:

  1. To fix product sorting issues:

    • Ensure proper context and method existence for fetching sort parameters.
    • Use built-in Magento functions like setOrder() instead of relying on custom methods.
  2. To manage simple product visibility:

    • Apply the ‘visibility’ attribute filter to include only catalog-visible products.
    • Set all relevant filters based on your catalog configuration.

Remember, understanding and leveraging Magento’s core functionality is key to building efficient and maintainable e-commerce solutions.

Catalog Themes Configuration Orders

Our network