skip to Main Content

I want to sort products for specific book category page woocommerce but i am unable to do that
below is the code i have applied but not worked for me

/*

add_filter('woocommerce_default_catalog_orderby','custom_catalog_ordering_args_func',220, 1);

   function custom_catalog_ordering_args_func($sortby)
   {
     $product_category = 'book'; // <== HERE define product category slug 

    if (! is_product_category($product_category)) {
        return;
    }else{
        return 'date';
    }
   
}

*/

2

Answers


  1. Use this code

    add_filter('woocommerce_default_catalog_orderby', 'sort_by_date', 10, 1);
    function sort_by_date($sortby){
    
        if (! is_product_category('book')) {
            return 'date';
        }
        return;
    }
    
    Login or Signup to reply.
  2. You can use woocommerce_default_catalog_orderby action hook. check the below code.

    add_filter( 'woocommerce_default_catalog_orderby', 'change_catalog_orderby_for_category' );
    function change_catalog_orderby_for_category( $sort_by ) {
        if( is_product_category('book') ) { 
            return 'date';
        }
        return $sort_by;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search