How to sort WooCommerce product by featured or tag without additional plugins?

2 min read 07-10-2024
How to sort WooCommerce product by featured or tag without additional plugins?


Sorting WooCommerce Products by Featured or Tag Without Plugins

Tired of relying on plugins to sort your WooCommerce products? You can achieve this functionality directly within your theme's functions.php file, providing a more streamlined and efficient solution. This article will guide you through the process of sorting products by "featured" status or by specific tags, without the need for any extra installations.

The Problem: WooCommerce offers default sorting options like price, popularity, and date. However, it doesn't provide out-of-the-box sorting by featured status or tags.

The Solution: We'll utilize the woocommerce_get_catalog_ordering_args filter to manipulate the default sorting behavior.

Scenario and Original Code:

Let's assume you want to create a custom sorting option labeled "Featured" that prioritizes featured products. Here's the basic code structure:

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_ordering_args', 10, 2 );
function custom_ordering_args( $args, $query ) {
    // Add your sorting logic here
    return $args;
}

Unique Insights and Analysis:

This filter allows you to modify the sorting arguments passed to the WooCommerce product query. Let's break down how to sort by featured status and tags:

1. Sorting by Featured Status:

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_ordering_args', 10, 2 );
function custom_ordering_args( $args, $query ) {
    if ( isset( $_GET['orderby'] ) && $_GET['orderby'] == 'featured' ) {
        $args['meta_query'] = array(
            array(
                'key' => '_featured',
                'value' => 'yes',
                'compare' => '=',
            ),
        );
        $args['orderby'] = array(
            'meta_value' => 'DESC',
            'date' => 'DESC', 
        ); // prioritize featured, then sort by newest
    }
    return $args;
}

This code snippet:

  • Checks if the 'orderby' parameter is set to 'featured'.
  • Adds a meta query to the $args array to filter products based on the '_featured' meta key.
  • Sets the orderby to 'meta_value' in descending order for featured products, followed by 'date' in descending order for non-featured products.

2. Sorting by Tag:

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_ordering_args', 10, 2 );
function custom_ordering_args( $args, $query ) {
    if ( isset( $_GET['orderby'] ) && $_GET['orderby'] == 'tag' ) {
        $tag_slug = sanitize_text_field( $_GET['tag'] ); // Assuming tag slug is passed via URL parameter 'tag'
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'product_tag',
                'field' => 'slug',
                'terms' => $tag_slug
            )
        );
        // Add additional orderby logic if needed (e.g., by price)
    }
    return $args;
}

This code snippet:

  • Checks if the 'orderby' parameter is set to 'tag'.
  • Extracts the tag slug from the URL parameter 'tag'.
  • Adds a tax query to the $args array to filter products based on the specified tag.
  • You can further customize the order by adding more orderby parameters.

SEO Optimization:

  • Use descriptive code comments to enhance readability and make your code easier to understand.
  • Consider adding a snippet in your functions.php file to display the sorting options in the shop page.
  • Ensure your product URLs follow a user-friendly structure that includes the tag slug when appropriate.

Additional Value:

You can extend this approach to sort products based on other custom meta fields, product categories, or combinations of criteria. This gives you more control over your product display and enhances the user experience by providing relevant sorting options.

References:

By implementing this approach, you gain greater control over your WooCommerce product sorting without relying on additional plugins. This fosters a streamlined setup and ensures your product listings remain organized and relevant to your customers.