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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.