Display items in order including image WooCommerce

I am trying to display the products/items within a WooCommerce order using the following php, but the items are not being shown.

The code I am using is an adaptation of this: Get cart item name, quantity all details woocommerce

Read More

Using the original code from the question above also does not display anything on my page.

Thanks in advance.

<?php  


        global $woocommerce;
        $items = $woocommerce->cart->get_cart();

        foreach($items as $item => $values) { 
        $_product = $values['data']->post;
        //product image
        $getProductDetail = wc_get_product( $values['product_id'] );
        echo '<tr><td>';
        echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr )
        echo '</td>';
        echo '<td>';
        echo '<p style="font-size:10pt;">'.$_product->post_title.'</p><td><p style="font-size:10pt;">x'. $values['quantity'] . '</p></td>'; 
        echo '</td></tr>';
        };
   ?>

The full page code is here:

<?php
/*
Template Name: Store
*/
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_die('This page is private.');


?>
  <!DOCTYPE HTML>
  <html>

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>
      <?php _e('Store Queue'); ?>
    </title>

  </head>

  <body id="driverqueue">
    <header>
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

        <h1 class="title"><?php the_title(); ?></h1>

        <?php the_content(); ?>

          <?php endwhile; endif; ?>
    </header>
    <section>
      <?php 

  global $woocommerce;


  $args = array(
    'post_type' => 'shop_order',
    'post_status' => 'wc-processing',
    'meta_key' => '_customer_user',
    'posts_per_page' => '-1'
  );
  $my_query = new WP_Query($args);

  $customer_orders = $my_query->posts;

  foreach ($customer_orders as $customer_order) {
   $order = new WC_Order();

   $order->populate($customer_order);
   $orderdata = (array) $order;

   // $orderdata Array will have Information. for e.g Shippin firstname, Lastname, Address ... and MUCH more.... Just enjoy!
  }




  $loop = new WP_Query( $args );

  while ( $loop->have_posts() ) : $loop->the_post();



    $order_id = $loop->post->ID;

    $order = new WC_Order($order_id);

    ?>







        <table class="ordertable" id="<?php echo $order_id; ?>">
          <tr>
            <td>
              <p>Order #
                <?php echo $order_id; ?> &mdash;
                  <time datetime="<?php the_time('c'); ?>">
                    <?php echo the_time('d/m/Y g:i:s A'); ?>
                  </time>
              </p>
            </td>
            <td>
              <?php echo $order->billing_first_name . ' ' . $order->billing_last_name ?>
            </td>
            <td>
              <table>
                <tr>

                  <?php  


            global $woocommerce;
            $items = $woocommerce->cart->get_cart();

            foreach($items as $item => $values) { 
            $_product = $values['data']->post;
            //product image
            $getProductDetail = wc_get_product( $values['product_id'] );
            echo '<tr><td>';
            echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr )
            echo '</td>';
            echo '<td>';
            echo '<p style="font-size:10pt;">'.$_product->post_title.'</p><td><p style="font-size:10pt;">x'. $values['quantity'] . '</p></td>'; 
            echo '</td></tr>';
            };
              ?>
                </tr>
              </table>
            </td>
            <td>
              <p>
                <?php
            do_action( 'woocommerce_admin_order_actions_start', $order );

            $actions = array();


            if ( $order->has_status( array( 'pending', 'on-hold', 'processing' ) ) ) {
              $actions['complete'] = array(
                'url'       => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=awaiting-shipment&order_id=' . $post->ID ), 'woocommerce-mark-order-status' ),
                'name'      => __( 'Complete', 'woocommerce' ),
                'action'    => "complete"
              );
            };

            $actions = apply_filters( 'woocommerce_admin_order_actions', $actions, $order );

            foreach ( $actions as $action ) {
              printf( '<a class="button %s" href="%s" data-tip="%s">%s</a>', esc_attr( $action['action'] ), esc_url( $action['url'] ), esc_attr( $action['name'] ), esc_attr( $action['name'] ) );
            }

            do_action( 'woocommerce_admin_order_actions_end', $order );
          ?>
              </p>
            </td>
            <td>
              <form action="">
                <input type="checkbox" class="ordercollected" value="0" />
              </form>
            </td>
          </tr>
          <?php endwhile; ?>

    </section>
  </body>

  </html>

Related posts

Leave a Reply

1 comment

  1. Solved this using info from the following link with the adapted code below:

    https://wordpress.stackexchange.com/questions/180075/how-to-get-woocommerce-order-product-info

    <?php  
    
    foreach ($order->get_items() as $key => $lineItem) {
    
            //uncomment the following to see the full data
                   // echo '<pre>';
                   // print_r($lineItem);
                   // echo '</pre>';
                    $product_id = $lineItem['product_id'];
                    $product = wc_get_product( $product_id );
                    echo '<tr><td>' . $product->get_image() . '</td>'; // accepts 2 arguments ( size, attr )
                    echo '<td>' . 'Product: ' . $lineItem['name'] . '</td>';
                    echo '<td> x' . $lineItem['qty'] . '</td></tr>';
                  }
    
                  ?>