I want to sorting product by discount percentage by high to low or low to high. I am using urna theme but sorting not working properly. I want it discount percentage wise if discount percentage same it will show price high to low or low to high. I using more sorting plugins. But its not working properly. Kindly help this. I am a beginner in woocommerce.
Please help
My code
<?php
add_filter( 'woocommerce_get_catalog_ordering_args', 'mycode_woocommerce_add_salediscount_to_catalog_ordering_args' );
function mycode_woocommerce_add_salediscount_to_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 ( 'discount' == $orderby_value ) {
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
$args['meta_key'] = '_dfrps_salediscount';
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'mycode_woocommerce_add_salediscount_to_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'mycode_woocommerce_add_salediscount_to_catalog_orderby' );
function mycode_woocommerce_add_salediscount_to_catalog_orderby( $sortby ) {
$sortby['discount'] = 'Sort by discount';
return $sortby;
}
2
Answers
You can try to use the following instead:
Code goes in functions.php file of the active child theme (or active theme). It should works.
You can try this code.Add this code in cureent active theme functions.php file.