skip to Main Content

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;
}

enter image description here

2

Answers


  1. You can try to use the following instead:

    add_filter( 'woocommerce_get_catalog_ordering_args', 'enable_catalog_ordering_by_meta' );
    function enable_catalog_ordering_by_meta( $args ) {
        $meta_key = '_dfrps_salediscount';
    
        if ( isset( $_GET['orderby'] ) ) {
            if ( 'custom_meta1' == $_GET['orderby'] ) {
                return array(
                    'orderby'  => 'meta_value_num',
                    'order'    => 'DESC',
                    'meta_key' => $meta_key,
                );
            }
            // Make a clone of "menu_order" (default option)
            elseif ( 'natural_order' == $_GET['orderby'] ) {
                return array( 'orderby'  => 'menu_order title', 'order' => 'ASC' );
            }
        }
    
        return $args;
    }
    
    add_filter( 'woocommerce_catalog_orderby', 'add_catalog_orderby_by_meta' );
    function add_catalog_orderby_by_meta( $orderby_options ) {
        // Insert "Sort by product reference (sku)" and the clone of "menu_order"
        return array(
            'custom_meta1'  => __("Sort by discount", "woocommerce");
            'natural_order' => __("Sort by natural order", "woocommerce"), // <== To be renamed at your convenience
        ) + $orderby_options ;
    }
    
    
    add_filter( 'woocommerce_default_catalog_orderby', 'default_catalog_orderby_meta' );
    function default_catalog_orderby_meta( $default_orderby ) {
        return 'custom_meta1';
    }
    
    add_action( 'woocommerce_product_query', 'product_query_by_meta' );
    function product_query_by_meta( $q ) {
        $meta_key = '_dfrps_salediscount';
        
        if ( ! isset( $_GET['orderby'] ) && ! is_admin() ) {
            $q->set( 'orderby', 'meta_value_num' );
            $q->set( 'order', 'DESC' );
            $q->set( 'meta_key', $meta_key);
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should works.

    Login or Signup to reply.
  2. You can try this code.Add this code in cureent active theme functions.php file.

    add_action( 'woocommerce_product_query', 'vzm_product_query_by_meta' );
    function vzm_product_query_by_meta( $q ) {
       $meta_key = 'staff_pick';   //your-meta key
       if ( ! isset( $_GET['orderby'] )) {
          $q->set( 'orderby', 'meta_value_num' );   // for numeric value
          $q->set( 'order', 'ASC' );
          $q->set( 'meta_key', $meta_key);
       }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search