Wp_query ordering problem

From what I understand the $productArrayShop is array of all the products ids.

So you first don’t need all the distances. what the point to get the distances? You just need all the products IDs and to order them by the meta_value.

BTW this order should be in the first loop instead of re-query for order.

/* So we don't need this. */
/*
foreach ( $productArrayShop as $id )
{
    $pdistance = $product_obj['current_distance']=get_post_meta($id,'current_distance');
    echo ' Distance=".$pdistance[0].", ';
    $arrayDistance[] = $pdistance[0];
}
*/

/**
* We dont need to loop for each distance we have already array for all the products IDs its all we need.
 */

$productd = array
(
    'post_type' => 'product',
    'posts_per_page'=> -1,
    'order' => 'ASC',
    'orderby' => 'meta_value',
    'meta_key' => 'current_distance',
    'post__in' => $productArrayShop // Here we get all the posts by the IDs with the right order.
);

$product_post = get_posts($productd);

// Then do something with the products...
print_r($product_post);

/**
 * You can loop to get each post object
 */
foreach($product_post as $p) {
    echo $p->ID;
}

Another thing to help you understand why you got the same IDs 81322. Its because you had 2 distances and in your second loop you select posts if they lower than the first one 62 and after this you select posts that lower than the second one 726.

Ofcource you get the same post. because if its match the first condition and it will match the second too. because you select the posts twice. and you order them with ASC (the lower first) so the same post was in index 0.

Woocommerce [products] shortcode custom orderby

First step you need to add filter to your functions.php that check if you pass the custom orderby value to the shortcode and to set the query args to the meta value that you want.

add_filter('woocommerce_shortcode_products_query', 'woo_products_sc_distance_order', 10, 3);
function woo_products_sc_distance_order($args, $atts, $type) {
    if ($atts['orderby'] == "distance") {
        $args['orderby']  = 'meta_value';
        $args['meta_key'] = 'current_distance';
    }
    return $args;
}

And then you call the shortcode with orderby distance

echo do_shortcode('[products ids="' . $result . '" per_page="8" columns="4" pagination="true" orderby="distance" order="ASC"]');

And basic usage:

[products orderby="distance"]