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 > '. $downloads .' </div>';
}}
To keep the question short and concise I have entered only the part of code concerned, omitting what believe should not affect. Anyway, here is the complete code: https://onecompiler.com/php/3xywvxhw7
Solution: Thanks to Michael Krikorev I found this solution that seems to work. It does not return any errors, warnings or notice message.
// Get product download button
$downloads = $order->get_downloadable_items();
if(is_array($downloads)){
foreach($downloads as $product){
$new_var = '<a href="'. $product['download_url'] .'" target="_blank">Download</a>';
}}
echo '<div > '. $new_var .' </div>'
CodePudding user response:
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 ><a href="'. $product['download_url'] .'" target="_blank">'. $product['product_name'] .' </a></div>';
} // END LOOP
} // END IF ARRAY