0

so i am customizing a plugin(wordpress site) for order notifications( sound notification and popup notification ), and i need to display the product items from the order.

Code:

    // get orders
    $recent_orders = wc_get_orders(array(
        'limit' => $show_order_num,
        'orderby' => 'date',
        'order' => 'DESC',
        'status' => $show_order_statuses,
    ));
    // begin table
    $content = "<h1>Нови порачки</h1>"; // New Orders
    $content .= "<table id='customers-new-order-notification'>";
    $content .= "<tr><th>Последни порачки</th></tr>"; // Last orders
    $content .= "<tr><th>Порачка бр.</th><th>Дата на порачка</th><th>Статус на порачка</th><th>Детали за порачка</th></tr>";
    // Order number - Date of Order - Order Status - Order Details

    foreach ($recent_orders as $recent_order) {
        
        $order_id = $recent_order->get_id();
        $_order = wc_get_order($order_id);
        $order_date = $_order->get_date_created();
        $order_status = $recent_order->get_status();
        $order_link = get_site_url();
        $order_link .= "/wp-admin/post.php?post=";
        $order_link .= $order_id;
        $order_link .= "&action=edit";
        $billing_first_name = $recent_order->get_billing_first_name() . ' ' . $recent_order->get_billing_last_name();
        $order_javena='Не јавено';

        $statusPrefix = "wc-";
        $_orderStatus = $statusPrefix . $order_status;
        $_order_status = $order_status_map[$_orderStatus];

        $date_format = get_option('date_format');
        $time_format = get_option('time_format');

        $format_order_date = $time_format . " - " . $date_format;
        $items = $recent_order->get_items();
        

        $content .= "<tr><td>" . esc_html($billing_first_name) . "</td><td>" . esc_html($order_date->date($format_order_date)) . "</td><td>" . esc_html($order_javena) . "</td><td><a href='" . esc_html($order_link) . "' target='_blank'>ПОГЛЕДНИ ДЕТАЛИ</a></td> .how to looop here. </tr>"; // how do i add a loop here to display all product items with a br
        
    }

    $content .= "</table><br><hr>";

I have tried the following way

 foreach ($recent_orders as $recent_order) {
        
        $order_id = $recent_order->get_id();
        $_order = wc_get_order($order_id);
        $order_date = $_order->get_date_created();
        $order_status = $recent_order->get_status();
        $order_link = get_site_url();
        $order_link .= "/wp-admin/post.php?post=";
        $order_link .= $order_id;
        $order_link .= "&action=edit";
        $billing_first_name = $recent_order->get_billing_first_name() . ' ' . $recent_order->get_billing_last_name();
        $order_javena='Не јавено';

        $statusPrefix = "wc-";
        $_orderStatus = $statusPrefix . $order_status;
        $_order_status = $order_status_map[$_orderStatus];

        $date_format = get_option('date_format');
        $time_format = get_option('time_format');

        $format_order_date = $time_format . " - " . $date_format;
        $items = $recent_order->get_items(); // get array
        

        $content .= "<tr><td>" . esc_html($billing_first_name) . "</td><td>" . esc_html($order_date->date($format_order_date)) . "</td><td>" . esc_html($order_javena) . "</td><td><a href='" . esc_html($order_link) . "' target='_blank'>ПОГЛЕДНИ ДЕТАЛИ</a></td>"
foreach ( $items as $item ) {
    $product_name = $item['name'];
    esc_html($product_name);
}
"</tr>";
        
    }

But i get a "syntax error, unexpected 'foreach' (T_FOREACH)"

How to add to a $variable a for loop?

TheFrisb
  • 32
  • 8

1 Answers1

0

You forgot to add semicolon just before nested foreach

 foreach ($recent_orders as $recent_order) {
        
        $order_id = $recent_order->get_id();
        $_order = wc_get_order($order_id);
        $order_date = $_order->get_date_created();
        $order_status = $recent_order->get_status();
        $order_link = get_site_url();
        $order_link .= "/wp-admin/post.php?post=";
        $order_link .= $order_id;
        $order_link .= "&action=edit";
        $billing_first_name = $recent_order->get_billing_first_name() . ' ' . $recent_order->get_billing_last_name();
        $order_javena='Не јавено';

        $statusPrefix = "wc-";
        $_orderStatus = $statusPrefix . $order_status;
        $_order_status = $order_status_map[$_orderStatus];

        $date_format = get_option('date_format');
        $time_format = get_option('time_format');

        $format_order_date = $time_format . " - " . $date_format;
        $items = $recent_order->get_items(); // get array
        
        // add semicolon at the end
        $content .= "<tr><td>" . esc_html($billing_first_name) . "</td><td>" . esc_html($order_date->date($format_order_date)) . "</td><td>" . esc_html($order_javena) . "</td><td><a href='" . esc_html($order_link) . "' target='_blank'>ПОГЛЕДНИ ДЕТАЛИ</a></td>";
       foreach ( $items as $item ) {
           $product_name = $item['name'];
           esc_html($product_name);
        }

       // append value in variable here
       $content .= "</tr>";
        
    }
Ankit Kumar
  • 139
  • 8