Woocommerce How To Disable Sort By

How to Disable “Sort By” Options in WooCommerce: A Comprehensive Guide

Introduction:

WooCommerce, the leading e-commerce platform for WordPress, offers a wide range of customization options to tailor your online store to your specific needs. One aspect that store owners often want to control is the “Sort By” functionality on product category and shop pages. While these options (like sorting by popularity, rating, price, or date) can be helpful for some customers, they might not align with your Learn more about How To See Sales Totals In Woocommerce overall sales strategy or desired user experience. For instance, you might want to exclusively showcase your products based on specific campaigns or hand-picked curated lists.

This article will guide you through various methods to disable or remove the “Sort By” options in WooCommerce, allowing you to maintain greater control over how your products are displayed. We’ll cover options ranging from simple code snippets to using plugin solutions, enabling you to choose the best approach for your skillset and requirements.

Main Body:

There are several ways to disable the “Sort By” dropdown in WooCommerce. We’ll explore the most common and effective methods:

1. Using Code Snippets (functions.php or a Code Snippet Plugin)

The most direct method involves adding a code snippet to your theme’s `functions.php` file or using a code snippet plugin (recommended for beginners to avoid directly editing theme files).

Important: Always back up your website before making any code changes. A small error can break your site. Using a child theme is also highly recommended to avoid losing your changes during theme updates.

Here’s the code to remove the “Sort By” dropdown completely:

 <?php /** 
  • Remove WooCommerce sorting
  • */ function remove_woocommerce_sorting() { remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 ); } add_action( 'after_setup_theme', 'remove_woocommerce_sorting' ); ?>

Explanation:

  • `remove_woocommerce_sorting()`: This defines a function that will remove the WooCommerce sorting.
  • `remove_action( ‘woocommerce_before_shop_loop’, ‘woocommerce_catalog_ordering’, 30 )`: This line specifically removes the action that renders the “Sort By” dropdown. `woocommerce_before_shop_loop` is the hook where the sorting is added, `woocommerce_catalog_ordering` is the function that generates the sorting dropdown, and `30` is the priority of the action.
  • `add_action( ‘after_setup_theme’, ‘remove_woocommerce_sorting’ )`: This ensures the `remove_woocommerce_sorting` function runs after your theme is fully loaded.

Steps:

1. Backup your website.

2. Access your WordPress dashboard.

3. Navigate to Appearance -> Theme Editor (or use a Code Snippets plugin).

4. If using the Theme Editor, locate the `functions.php` file for your *child theme*.

5. Paste the code snippet at the end of the file.

6. Click “Update File”.

2. Using CSS to Hide the Element (Not Recommended for Accessibility)

While not the ideal solution, you can technically hide the “Sort By” element using CSS. This method only hides the element visually; it doesn’t remove the functionality entirely and isn’t good for accessibility. Users with assistive technologies (screen readers) might still be able to interact with it.

.woocommerce-ordering {

display: none !important;

}

Steps:

1. Access your WordPress dashboard.

2. Navigate to Appearance -> Customize -> Additional CSS.

3. Paste the CSS code.

4. Click “Publish”.

Why this isn’t recommended:

  • Accessibility issues: Screen readers can still interact with the element.
  • Doesn’t actually remove the functionality: Technically, the sorting options still exist in the background.
  • Overreliance on `!important`: Using `!important` can lead to cascading style issues later on.

3. Using a WooCommerce Customization Plugin

Several plugins offer WooCommerce customization options, including the ability to disable or modify the “Sort By” feature. These plugins usually provide a user-friendly interface without requiring you to write any code. Search the WordPress plugin repository for terms like “WooCommerce customization,” “WooCommerce enhancements,” or similar phrases.

Advantages of Using a Plugin:

  • Ease of use: No coding required.
  • Potentially more options: Plugins might offer more granular control over the “Sort By” functionality.
  • Updates and support: Plugin developers typically provide updates and support.

Disadvantages of Using a Plugin:

  • Plugin bloat: Adding too many plugins can slow down your site.
  • Compatibility issues: Plugins might conflict with other plugins or your theme.
  • Cost: Some customization plugins are premium (paid).

4. Filtering the `woocommerce_catalog_orderby` Array (For More Control)

If you want to selectively remove certain “Sort By” options instead of disabling the entire dropdown, you can filter the `woocommerce_catalog_orderby` array.

 <?php /** 
  • Remove specific sorting options from WooCommerce
  • */ function custom_woocommerce_catalog_orderby( $sortby ) { unset( $sortby['popularity'] ); // Remove "Sort by popularity" unset( $sortby['rating'] ); // Remove "Sort by average rating" // unset( $sortby['date'] ); // Remove "Sort by newness" // unset( $sortby['price'] ); // Remove "Sort by price: low to high" // unset( $sortby['price-desc'] ); // Remove "Sort by price: high to low"

    return $sortby;

    }

    add_filter( ‘woocommerce_catalog_orderby’, ‘custom_woocommerce_catalog_orderby’ );

    ?>

    Explanation:

    • `custom_woocommerce_catalog_orderby( $sortby )`: This function takes the `$sortby` array as input. This array contains all the available sorting options.
    • `unset( $sortby[‘popularity’] )`: This line removes the “Sort by popularity” option from the array. Repeat this line for each sorting option you want to remove, uncommenting the relevant line.
    • `return $sortby`: This returns the modified `$sortby` array.
    • `add_filter( ‘woocommerce_catalog_orderby’, ‘custom_woocommerce_catalog_orderby’ )`: This applies the filter to the `woocommerce_catalog_orderby` hook.

    To find the correct keys (e.g., ‘popularity’, ‘rating’), you can temporarily add the following code to your `functions.php` to display the `$sortby` array:

     add_action( 'woocommerce_before_shop_loop', function() { global $wp_query; $ordering_args = $wp_query->get( 'orderby' ); echo '
    '; print_r( wc_get_catalog_ordering_options() ); echo '

    ';
    });

    This will output the array on your shop page, allowing you to see the keys for each option. Remember to remove this debug code after you've identified the keys.

    Conclusion:

    Disabling or customizing the "Sort By" options in WooCommerce is a straightforward process that can significantly impact your store's user experience and sales strategy. While CSS offers a quick visual fix, it's not recommended due to accessibility concerns. Code snippets provide a clean and effective solution, allowing you to completely remove or selectively filter the sorting options. Plugins offer a user-friendly alternative for those who prefer a no-code approach.

    Choose the method that best suits your technical expertise and the specific needs of your WooCommerce store. Remember to always back up your website and use a child theme before making any changes to the code. By carefully controlling how your products are displayed, you can optimize your store for conversions and create a more cohesive and controlled shopping experience for your customers.

    Comments

    No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *