Woocommerce Custom Sorting Showing Different Results on Refresh

Question

I’ve added a custom orderby for woocommerce however everytime I refresh it’s bringing back different results! Not sure why the values stored in the DB are 1,2,3,4,5 etc all the way to 418. I am displaying the products on the page using the foreach method not WP_Query.

function skyverge_add_postmeta_ordering_args( $sort_args ) {
    
    $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    $sort_args['orderby']  = 'meta_value_num';
    $sort_args['order']    = 'asc';
    $sort_args['meta_key'] = 'lot_number';
    return $sort_args;
} add_filter( 'woocommerce_get_catalog_ordering_args', 'skyverge_add_postmeta_ordering_args' );


// Add these new sorting arguments to the sortby options on the frontend
function skyverge_add_new_postmeta_orderby( $sortby ) {
    $sortby['lotnumber'] = __( 'Sort by Lot Number', 'woocommerce' );
    return $sortby;
}

add_filter( 'woocommerce_default_catalog_orderby_options', 'skyverge_add_new_postmeta_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'skyverge_add_new_postmeta_orderby' );
0
Daniel Vickers 2 months 0 Answers 8 views 0

Leave an answer