skip to Main Content

I am attempting to order my products by Length and Width, where it sorts by length first and, if two products have the same value, it will sort by Width (and so on). I would like to add height if I can get this working first.

So far the issue I’m running into is that it orders the products by length and only length (plus a bug where even the length is incorrect, i.e. the product order is 2.1, 2, and 2.5).

This is as far as I’ve gotten with the query:

function woocommerce_catalog_orderby( $args ) {
    $args['meta_query'] = array(
        'relation' => 'AND',
        'length_clause' => array(
            'key'     => '_length',
            'type' => 'DECIMAL',
            'orderby' => 'meta_value_num',
        ),
        'width_clause' => array(
            'key'     => '_width',
            'type' => 'DECIMAL',
            'orderby' => 'meta_value_num',
        ),
    );

    $args['orderby'] = array(
        'length_clause' => 'ASC',
        'width_clause' => 'ASC',
    );

    return $args;
}
add_filter('woocommerce_get_catalog_ordering_args', 'woocommerce_catalog_orderby');

I’m convinced I’m missing something obvious that I just can’t see at the moment. Any help is much appreciated!

2

Answers


  1. Custom sorting can be added using: please follow the step according and replace your meta value

    https://docs.woocommerce.com/document/custom-sorting-options-ascdesc/

    Login or Signup to reply.
  2. In your case you must add priority when you add a filter for woocommerce.
    Here is the working code for sorting by meta values:

    /**

    '''
     function wh_save_product_custom_meta($post_id, $post, $update) {
            $post_type = get_post_type($post_id);
            // If this isn't a 'product' post, don't update it.
            if ($post_type != 'product')
                return;
        
            if (!empty($_POST['attribute_names']) && !empty($_POST['attribute_values'])) {
                $attribute_names = $_POST['attribute_names'];
                $attribute_values = $_POST['attribute_values'];
                foreach ($attribute_names as $key => $attribute_name) {
                    switch ($attribute_name) {
                        //for color (string)
                        case 'pa_color':
                            //it may have multiple color (eg. black, brown, maroon, white) but we'll take only the first color.
                            if (!empty($attribute_values[$key][0])) {
                                update_post_meta($post_id, 'pa_color', $attribute_values[$key][0]);
                            }
                            break;
                        //for lenght (int)
                        case 'pa_length':
                            if (!empty($attribute_values[$key][0])) {
                                update_post_meta($post_id, 'pa_length', $attribute_values[$key][0]);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        
        add_action( 'save_post', 'wh_save_product_custom_meta', 10, 3);
    '''   
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search