Answer the question
In order to leave comments, you need to log in
How to change default sorting in Woocommerce?
I add my plugin to change sorting. On the pages of the catalog there is a new item in the selection of sorting - "Innovative". When selected, the get parameter is set and sorting works as it should. I hang adding sorting on the woocommerce_product_query hook, because meta_query is not passed in the argument from the woocommerce_get_catalog_ordering_args hook...
After I set the default sorting to "Innovative", the sorting is not applied... But the default is the selected new sorting.
The woocommerce_product_query hook fires, the condition passes, additional query parameters are set, but apparently the products have already been received at that time ... Is there a hook for getting products that fires before the custom_woocommerce_product_query hook?
<?php
function custom_woocommerce_get_catalog_ordering_args($args) {
$orderby_value = isset($_GET['orderby'])
? woocommerce_clean($_GET['orderby'])
: apply_filters(
'woocommerce_default_catalog_orderby',
get_option('woocommerce_default_catalog_orderby')
);
if ('innovation' == $orderby_value) {
$args = array(
'orderby' => array(
'by_availability' => 'ASC',
'by_popularity' => 'DESC'
),
);
}
return $args;
}
function custom_woocommerce_catalog_orderby($sortby) {
$sortby['innovation'] = 'Инновационная';
return $sortby;
}
function custom_woocommerce_product_query($query)
{
$meta_query = $query->get('meta_query');
if (get_option('woocommerce_default_catalog_orderby') == 'innovation') {
$meta_query[] = array(
'by_availability' => array(
'key' => '_stock_status',
));
$meta_query[] = array(
'by_popularity' => array(
'key' => 'total_sales',
'type' => 'NUMERIC',
));
$query->set('meta_query', $meta_query);
}
}
add_filter(
'woocommerce_get_catalog_ordering_args',
'custom_woocommerce_get_catalog_ordering_args',
20
);
add_filter(
'woocommerce_default_catalog_orderby_options',
'custom_woocommerce_catalog_orderby',
20
);
add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby', 20);
add_action('woocommerce_product_query', 'custom_woocommerce_product_query' );
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question