0

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?

Community
  • 1
  • 1
JackTheKnife
  • 3,795
  • 8
  • 57
  • 117

2 Answers2

0

Got it solved

First I have assigned keys to the $data product array

    $data[$product->id] = array(
                      "permalink" => $product_permalink,
                      "title" => $product_title,
                      "thumbnail_url" => $product_thumbnail_url[0], 
                      "description" => $product_description,
                      "price" => $product_price,
                      "in_cart" => $product_cart,
                      "id" => $product_id);

As I have already assigned product_id as a key for $data array I was able to sort using a loop below

$sortedProducts = [];

foreach ($sortingArr as $id) {
    $sortedProducts[] = $data[$id];
}

In case someone don't have $data array with related keys but you have them stored inside that array you can always use array_column to reindex your array

// array_column is only available in PHP 5.5+
$data = array_column($data, null, 'id');

and then sort using a method described above.

JackTheKnife
  • 3,795
  • 8
  • 57
  • 117
-1

You have a $data array OF ARRAYS with $product->id keys. You cannot therefore search the $data array, as your needle would have to be an array itself (more exactly pointer to the same array)

Considering your code assigned product ID on position 6 (7th element), you can replace

//sorting
foreach($sortingArr as $val){ 
    $result[array_search($val, $data)] = $val; 
}

for

// sorting
foreach($sortingArr as $val){ 
    // $data is an array of arrays, so you must look through item in position $data[$i][6], where product ID is located in your case
    foreach($data as $product_id => $d){
        // if present here
        if(array_search($val, $d[6]) !== false){
            $result[$product_id] = $d;

            // we have a match, no need to loop more
            break;
        }
    }  
}
michnovka
  • 2,880
  • 3
  • 26
  • 58