WooCommerce Code Snippet: Easily Change default sorting options on catalog page to Alphabetical & Date Ascending / Descending

Are you looking to provide your WooCommerce customers with more granular control over how products are displayed on your catalog page? In this tutorial, we’ll explore a simple WooCommerce code snippet that allows you to replace the default sorting options with alphabetical sorting and ascending/descending date created. This customization can significantly enhance user experience and make it easier for customers to find the products they’re looking for.

Code Breakdown:

The provided code snippet leverages the woocommerce_catalog_orderby filter to modify the available sorting options on the catalog page. Here’s a breakdown of what it does:

Removes Default Options: The code starts by removing the existing sorting options to ensure a clean slate for our customizations.
Adds four new sorting options:
– Alphabetical (A-Z): Sorts products by their titles in ascending alphabetical order.
– Alphabetical (Z-A): Sorts products by their titles in descending alphabetical order.
– Date Published (Ascending): Sorts products by their publication date in ascending order.
– Date Published (Descending): Sorts products by their publication date in descending order.
Returns Modified Options: Finally, the function returns the modified array of sorting options, which will be used by WooCommerce to populate the sorting dropdown on the catalog page.
Implementation:

By customizing the WooCommerce catalog sorting options, you can provide your customers with a more tailored shopping experience. This can lead to increased user satisfaction, improved conversions, and ultimately, a more successful online store.

Code has been tested as working in WooCommerce version 9.3.2.

WooCommerce Code Snippet:

/**
 * Replace default sorting options on the catalog page.
 *
 * @param array $options Sorting options.
 * @return array Modified sorting options.
 */
function custom_catalog_ordering( $options ) {

    // Remove existing sorting options.
    $options = array();

    // Add custom sorting options.
    $options['title']  = __( 'Sort alphabetically (A-Z)', 'woocommerce' );
    $options['title-desc'] = __( 'Sort alphabetically (Z-A)', 'woocommerce' ); 

    // Sort by date published (using 'post_date' for consistency with 'the_date' function)
    $options['post_date'] = __( 'Sort by date published (Ascending)', 'woocommerce' ); 
    $options['post_date-desc'] = __( 'Sort by date published (Descending)', 'woocommerce' ); 

    return $options;
}
add_filter( 'woocommerce_catalog_orderby', 'custom_catalog_ordering' );

How should I implement this code?

To implement this WooCommerce code snippet, you have two options:
Child Theme’s functions.php: If you’re using a child theme, you can simply paste the code into your child theme’s functions.php file. This ensures that your customizations will be preserved even if you update your parent theme.
Code Snippet Plugin: Alternatively, you can use a code snippet plugin like WPCode to manage your custom code. This can be a convenient option if you need to manage multiple code snippets or if you prefer a more organized approach.

If you need any further help with customizing WooCommerce, please feel free to contact us for a free consultation.

Item added to cart.
0 items - $0.00