PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, May 10, 2022

[FIXED] How do I manually set WooCommerce product sorting to "Popularity"?

 May 10, 2022     php, product, sorting, woocommerce, wordpress     No comments   

Issue

In WooCommerce, I'm using the following code to set the default sorting to order by date for a specific product category archive page:

add_filter('woocommerce_default_catalog_orderby', 'custom_catalog_ordering_args', 20, 1);
function custom_catalog_ordering_args($sortby)
{
    $product_category = 'specials'; // <== HERE define your product category slug 

    // Only for defined product category archive page
    if (! is_product_category($product_category)) {
        return;
    }
    return 'date';
}

However, this is affecting my overall default sorting settings "by popularity", as when I look on my shop page it is sorted incorrectly but if I manually change it to sort by something else and then back it sorts it correctly.

How do I fix this issue or how do I manually set the rest of the shop to order by Popularity with php as this may fix the issue?


Solution

Updated: With a filter hook you need always to return the first function argument variable, and not just return alone without a value or a the default function variable argument… So in your code:

add_filter('woocommerce_default_catalog_orderby', 'custom_catalog_ordering_args', 10, 1);
function custom_catalog_ordering_args( $orderby )
{
    $product_category = 'specials'; // <== HERE define your product category slug 

    // For all other archives pages
    if ( ! is_product_category($product_category)) {
        return $orderby; // <====  <====  <====  <====  <====  HERE
    }
    // For the defined product category archive page
    return 'date'; 
}

OR better this way:

add_filter('woocommerce_default_catalog_orderby', 'custom_catalog_ordering_args', 10, 1);
function custom_catalog_ordering_args( $orderby ) {
    // HERE define your product category slug
    $product_category = 'specials';  

    // Only for the defined product category archive page
    if ( is_product_category($product_category)) {
        $orderby = 'date'; 
    }
    return $orderby; 
}

It should work now.

Related:

  • Override Avada Catalog sorting hook back to default in Woocommerce
  • Change default sorting for specific Woocommerce product category archive pages


Answered By - LoicTheAztec
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing