• Resolved wurwaldesign

    (@wurwaldesign)


    I would like to create a custom woocommerce product orderby sort option that combines the values of one custom numeric field for a custom product sort option.

    – ‘sort_price’

    with another numeric field (woocommerce regular price)

    – ‘_price’

    And order the combined values in ASC order

    My code so far achieves ordering by sort_price alone but I am unsure how to combine the two.

    
    add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
    
    function custom_woocommerce_get_catalog_ordering_args( $args ) {
      $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    	if ( 'random_list' == $orderby_value ) {
    		$args['orderby'] = 'meta_value_num';
    		$args['order'] = 'ASC';
    		$args['meta_key'] = 'sort_price';
    	}
    	return $args;
    }
    add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
    add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
    function custom_woocommerce_catalog_orderby( $sortby ) {
    	$sortby['random_list'] = 'Price Low to High Custom';
    	return $sortby;
    }
    

    there is an example in the wp codex which demonstrates using multiple meta keys but I am a little unsure of what the compare and value parts in the array are doing.

    
        'meta_query' => array(
            'relation' => 'AND',
            'state_clause' => array(
                'key' => 'state',
                'value' => 'Wisconsin',
            ),
            'city_clause' => array(
                'key' => 'city',
                'compare' => 'EXISTS',
            ), 
        ),
        'orderby' => array( 
            'city_clause' => 'ASC',
            'state_clause' => 'DESC',
        ),
    
    • This topic was modified 3 years, 10 months ago by wurwaldesign.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Combining multiple numeric meta_key’s in an order_by query’ is closed to new replies.