Home:ALL Converter>Woocommerce order by custom callback function

Woocommerce order by custom callback function

Ask Time:2016-06-22T00:02:57         Author:Illes Peter

Json Formatter

I added a new sorting option to Woocommerce, which would sort by the lowest price. All my prices are stored in a custom attribute, along with some other serialized data. What I want is to have a callback function which would unserialize this data, check for the lowest price and sort by that price.

Every post I saw about custom ordering options, makes use of the woocommerce_get_catalog_ordering_args filter to add orderby arguments to WP_Query, but I haven't seen any way to sort the results of the returned query via a callback function.

Is there any way to achieve this? I was looking at woocommerce_after_product_ordering based on the name, but couldn't find much info about it. Any help would be much appreciated!

function my_woocommerce_product_sorting($orderby) {
    $orderby['price_asc'] = "Sort by cheapest";
    $orderby['price_desc'] = "Sort by most expensive";

    return $orderby;
}
add_filter("woocommerce_catalog_orderby", "my_woocommerce_product_sorting", 20);

Author:Illes Peter,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37949357/woocommerce-order-by-custom-callback-function
yy