Magento 2.4.0: Custom Sorting Function setOrder Not Working
As a seasoned Magento developer, I often encounter unique challenges when working on custom sorting functions for product lists in the latest Magento versions. In this post, we’ll dive into an issue affecting Magento 2.4.0 where the built-in setOrder function seems to fail when implemented for custom sorting options.
The Problem: setOrder Not Adding Sort Conditions
You’ve likely tried implementing custom sorting functions by modifying the core files and using the setOrder method, only to find that your queries don’t include any actual sort conditions. When you print out the SQL query generated by Magento, you’ll notice it contains a blanket WHERE null clause instead of the expected sorting conditions.
The primary culprits for this issue are located in two key files:
\vendor\magento\module-catalog\Block\Product\ProductList\Toolbar.php\vendor\magento\module-catalog\Model\Config.php
Understanding the Core Files
Toolbar.php: The Collection Setup
The Toolbar class is responsible for setting up and initializing the product collection used in the list view. When you override this class, ensure that your changes are applied correctly within the default constructor or the setCollection method.
In Magento 2.4.0, the core class might not be handling custom sorting options as expected due to internal changes or optimizations introduced with the new version.
Config.php: The Sorting Options Configuration
The Config model determines which attributes are available for sorting and their corresponding labels in the frontend. When adding new sorting options via a plugin, you must ensure that these options are correctly integrated into the configuration array returned by the getAttributeUsedForSortByArray method.
Your Current Implementation: A Closer Look
You’ve already attempted to implement custom sorting options through a combination of di.xml and PHP plugins targeting both the Config model and the Toolbar class. Here’s a summary of your current setup:
di.xml Configuration
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\Config">
<plugin name="Evolve_Sorting::addCustomSortOptions" type="Evolve\Sorting\Plugin\Model\Config" />
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="Evolve_Sorting::implementCustomSortOptions" type="Evolve\Sorting\Plugin\Product\ProductList\Toolbar" />
</type>
</config>
Config.php Plugin
namespace Evolve\Sorting\Plugin\Model;
class Config
{
// ... (other methods)
public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
{
// Add custom options and change labels
// ...
return $options;
}
}
Toolbar.php Plugin
namespace Evolve\Sorting\Plugin\Product\ProductList;
class Toolbar
{
// ... (other properties)
public function aroundSetCollection(
\Magento\Catalog\Block\Product\ProductList\Toolbar $toolbar,
\Closure $proceed,
$collection
)
{
// Apply custom sorting logic based on the current order
// ...
}
}
The Root Cause and Solution
The issue lies in how Magento 2.4.0 processes and applies custom sorting options, especially when using plugins to extend core functionality.
To resolve this problem effectively:
-
Review your plugin structure: Ensure that both your
ConfigandToolbarplugins are correctly structured and follow the recommended best practices for Magento plugins. -
Check your sort condition implementation: Verify that you’re using the appropriate methods (
addAttributeToSort,getSelect()->order) to apply sorting conditions to your product collection, as per Magento’s documentation and coding standards. -
Test in isolation: Create a dedicated module or use a separate development environment to test your custom sorting functionality without interference from other extensions or core files.
Conclusion: Addressing the Issue
In conclusion, the setOrder method failing to add sorting conditions in Magento 2.4.0 when implementing custom options is likely due to changes introduced with the latest version or conflicts with other extensions.
To solve this issue:
- Thoroughly review your plugin structure and implementation.
- Ensure you’re using the correct methods for applying sorting conditions to the product collection.
- Test your custom sorting functionality in isolation to identify any potential issues or conflicts.
If you continue facing challenges, I recommend reaching out to Magento’s official support channels or seeking guidance from experienced developers in the community.
Remember, keeping up-to-date with the latest Magento releases and their changes is crucial for maintaining a stable and optimized e-commerce platform.