Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

239
Vistas
Woocommerce - How to recognize string as array with echo (php)

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>';
}}

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 class="container_orders_download"> '. $new_var .' </div>'

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda