skip to Main Content

I have a function that adds a new field to order by on WooCommerce. I would like to display the result for products that have the ‘bestseller’ select box checked.
ACF:
enter image description here

function cw_add_postmeta_ordering_args( $args_sort_cw ) {
 
 $cw_orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) :
       apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
 switch( $cw_orderby_value ) {
   case 'bestseller':
     $args_sort_cw['orderby'] = 'meta_value';
     $args_sort_cw['order'] = 'desc';
     $args_sort_cw['meta_key'] = 'bagdes';
     $args_sort_cw['meta_value'] = 'bestseller'; 
     break;
 
 }
 
 return $args_sort_cw;
}

Thanks in advance
Regards.

2

Answers


  1. Chosen as BEST ANSWER

    Good concept but it doesn't work for me my friend. I also changed:

    'compare' => 'LIKE',
    

    Unfortunately, it did not help.


  2. I changed the coed using a 'meta_query' with the corrected ACF field key ‘badges‘, and adjusted sorting parameters that have the ‘bestseller‘ select box checked, hope that helps

    function cw_add_postmeta_ordering_args( $args_sort_cw ) {
        $cw_orderby_value = isset($_GET['orderby']) ? wc_clean($_GET['orderby']) :
            apply_filters('woocommerce_default_catalog_orderby', get_option('woocommerce_default_catalog_orderby'));
    
        switch ($cw_orderby_value) {
            case 'bestseller':
                $args_sort_cw['orderby'] = 'meta_value';
                $args_sort_cw['order'] = 'DESC';
                $args_sort_cw['meta_query'] = array(
                    array(
                        'key'     => 'badges', //Ensure this is your correct ACF field key
                        'value'   => 'bestseller',
                        'compare' => '=',
                    ),
                );
                break;
        }
    
        return $args_sort_cw;
    }
    add_filter('woocommerce_get_catalog_ordering_args', 'cw_add_postmeta_ordering_args');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search