I'm pulling all available products in WooCommerce using code below
$sortingArr = $my_cart;
$data = array();
$result = array();
if ($wc_query->have_posts()) :
while ($wc_query->have_posts()) :
$wc_query->the_post();
$product = new WC_Product(get_the_ID());
$product_permalink = get_permalink();
$product_title = get_the_title();
$product_thumbnail_id = get_post_thumbnail_id();
$product_thumbnail_url = wp_get_attachment_image_src( $product_thumbnail_id, 'medium', true );
$product_description = apply_filters('the_content',get_the_excerpt());
$product_price = $product->get_price_html();
$product_cart = $product->single_add_to_cart_text();
$product_id = $product->id;
$data[$product->id] = array(
$product_permalink,
$product_title,
$product_thumbnail_url[0],
$product_description,
$product_price,
$product_cart,
$product_id);
endwhile;
wp_reset_postdata();
//sorting
foreach($sortingArr as $val){
$result[array_search($val, $data)] = $val;
}
foreach ($result as $value) {
<!-- displaying HTML output -->
}
endif;
Now I'm trying to sort those products by products ID which are currently in the cart using usort()
or asort()
functions and methods described here. All is working fine when I will remove sorting code and display $data
array.
EDIT:
$data
array has keys related to Product ID which may help to sort by another array of Product IDs based on what is inside the cart.
Any tips what I do wrong?