0

I have a shortcode for woocommerce that shows all orders placed by a customer, it works. However, I came to a piece of code that gives me the following message:

Notice: Array to string conversion in /home/vwzidcur/public_html/wp-content/themes/astra-child/woocommerce/woo-shortcodes.php on line 67. On line 67 of my file i have echo.

Googling and reading the official php manual I came to the conclusion that the problem can be solved with implode or by iterating $downloads array with foreach. I tried but couldn't. I am relatively new to this, any help would be appreciated.

$customer = wc_get_orders(['customer_id' => get_current_user_id(),]);

foreach ( $customer as $order ) {
foreach ( $order->get_items() as $item_id => $item ) {

$downloads = $order->get_downloadable_items();

  if ($downloads){ 
  wc_get_template(
    'button-downloads-dashboard.php',
     array(
     'downloads'  => $downloads,
     ));
     }

echo '<div class="container_orders_img"> '. $downloads .' </div>';
}}
1
  • Do a print_r($downloads) and update the question with what the variable contains, then I can try helping with the loop. Commented Apr 12, 2022 at 16:53

1 Answer 1

3

Try something like this;

// FIRST ALWAYS CHECK IF ITS AN ARRAY
if(is_array($downloads)){
  // START THE LOOP
  foreach($downloads as $product){
    // HERE YOU CHOOSE WHAT TO PRINT OF ALL THE DIFFERENT VARIABLES IN THE ARRAY
    echo '<div class="container_orders_img"><a href="'. $product['download_url'] .'" target="_blank">'. $product['product_name'] .' </a></div>';
  } // END LOOP
} // END IF ARRAY
Sign up to request clarification or add additional context in comments.

4 Comments

Inserted like this it works. However i need to keep echo '<div class="container_orders_img"><a href="'. $product['download_url'] .'" target="_blank">'. $product['product_name'] .' </a></div>'; out of the foreach loop because i am building some tabs in css, i need to echo somewhere else and not inside loop. Is there any way to move it ?
I'm not that familiar with Wordpress or WooCommerce and don't have the full picture, but it shouldn't be a problem. If the method $order->get_downloadable_items() need to be inside the loop to work, then you could do the loop in place and store the output in a new variable instead of echoing it out.
As I'm relatively new to php, could you kindly point me to a code example of how to store the output in a new variable and loop just like you said ?
Okay, nothing, I did it. Place the example by modifying the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.