简体   繁体   中英

Woocommerce sort products by category using hook or filter

Want to sort product with the help of custom category using hook/filter in functions.php

For instance: I have a product "A" with 2 category "featured" and "most popular" and i have another product "B" on the same page which only has "featured" category assigned to it.

I want to be able to sort this 2 products in such a way that all products with "most popular" category should come first and the rest with only "featured" category will be displayed after that.

Need this done using functions.php hooks or filter. Here is the one i found useful but i only knows how to sort it with date.

add_filter('woocommerce_get_catalog_ordering_args', 'catalog_ordering_args');

function catalog_ordering_args($args) {
    $args['orderby'] = 'date';
    $args['order'] = 'desc';
    return $args;
}

Anyone struggling todo this, i have the solution.

I didn't know this before(wp noob.!) but every product has a Menu order field in advanced tab which is set to zero(0).

I used a "PW Bulk Edit" Plugin to change this value to -1 for my specific category product and all the product were sorted correctly. (ie: products with menu order value -1 will sort higher and products with value 0 will be sorted after that)

Note: lower the value is higher the position of the product.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM