Sorting Magento Products by SKU
Hello everyone! Today we’re going to walk through the process of sorting products in your Magento store by their Stock Keeping Unit (SKU) values. We’ll be covering two main aspects:
- Setting the default sort option to SKU.
- Adding an option for users to manually select and sort by SKU.
Let’s dive right into it!
1. Setting the Default Sort Option to SKU
By default, Magento sorts products alphabetically by name on category pages. To change this to SKU order instead:
Step 1: Open your theme file
Navigate to your active theme folder under app/design/frontend/{vendor}/{theme}/Magento_Catalog/templates/product/list.phtml. This is where the product listing code resides.
Step 2: Modify the sort options array
In this file, you’ll find a line that looks like:
$sortOrder = $block->getAvailableOrders();
Change this to:
$sortOrder = [
'sku' => 'Sku',
'name' => 'Name'
];
We’ve moved the SKU option above the name.
Step 3: Update the sort order ID
Further down, look for the line that sets the default sort order ID:
$defaultSort = $block->getCurrentOrder();
Update it to:
$defaultSort = 'sku';
That’s it! Save your file and clear your cache. Now your category pages will display products sorted by SKU in ascending order by default.
2. Adding a User-Selectable SKU Sort Option
To give users the ability to manually sort by SKU:
Step 1: Open the toolbar view
Navigate to app/design/frontend/{vendor}/{theme}/Magento_Catalog/templates/product/tools.phtml.
Step 2: Add the SKU option to the toolbar dropdown
Find this line:
$availableOrders = $block->getAvailableOrders();
Just above it, add these lines:
$sortOrder = [
'sku' => 'Sku',
'name' => 'Name'
];
$block->setAvailableOrders($sortOrder);
This will ensure the SKU option is available.
Step 3: Update the toolbar rendering
Scroll down to where you see this loop:
foreach ($availableOrders as $order) {
// Existing code here...
}
Add an if statement before the foreach loop:
if (count($availableOrders) > 1) { // Only show options if there's more than one available
?>
<div class="view-control">
<?php echo $block->getToolbarHtmlConditionGetName() ?>
<label for="mode"><?php /* @escapeNotVerify */ echo __('Sort By') ?></label>
<select id="mode" onchange="<?php /* @escapeNotVerify */ echo $block->getToolbarHtmlConditionPostName() ?>">
<?php foreach ($availableOrders as $order => $title) : ?>
<option value="<?php /* @escapeNotVerify */ echo $order ?>" <?php if (($currentOrder == $order): ?> selected="selected"<?php endif; ?>><?php /* @escapeNotVerify */ echo $title ?></option>
<?php endforeach; ?>
</select>
</div>
<?php
}
This will only display the sort options dropdown when multiple options are available.
That’s it! Save your file and clear your cache. Now users can select to sort by SKU on category pages.
Conclusion
In this post, we’ve covered how to modify Magento’s default behavior to sort products by SKU order and provide this as a user-selectable option. By following the steps outlined above, you should have a well-organized product listing experience that makes it easy for both your admin team and customers to search by SKU.
Remember, when working with Magento themes, always work from a child theme to preserve your changes across updates. For more information on best practices for Magento development, check out our comprehensive guide at https://laravelcompany.com/magento-development-best-practices.
Happy coding! Let me know if you have any questions in the comments below.