0

is there a way to order products by price only when a user enters to a specific category?

I kind of know that I can achieve this using hooks in functions.php but I'm not sure on how to achieve this

add_filter('function-for-ordering-products','my-custom-function');

function my-custom-function(){

//Step 1. Find the category the user is currently watching
//Step 2.  If the category is "Giftcards" go to step 3
//Step 2.1 If the category is not "Giftcards" go to step 4
//Step 3. Set products order by price
//Step 4. Do nothing

}

Can you help me out with that?

In Add_filter do i need to use woocommerce_before_shop_loop?

So the correct filter would be...

add_filter('woocommerce_before_shop_loop ','my-custom-function')?

1

You can use the filter below, as suggested by @sabarnix at

https://stackoverflow.com/questions/24939337/woocommerce-different-default-sorting-for-different-categories

add_filter('woocommerce_get_catalog_ordering_args', 'tk_woocommerce_catalog_orderby');
function tk_woocommerce_catalog_orderby( $args ) {
    if( is_product_category( 'shirts' ) ) {
        $args['orderby']  = 'meta_value_num';
        $args['order']    = 'ASC';
        $args['meta_key'] = '_price'; 
    }
    return $args;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.