Show product’s image in “Orders” page – Woocommerce

I am working with WC for the first time and I am chasing my tail with this one.

In “Orders” page where customer’s can see all the purchases they’ve made the array shows some basic info about the order.
I need to also show the image of the product the customer bought.

Read More

Orders seems to be custom posts, but how do I get the image product?

I realize the question is too vague. Any pointers would be a big great help.

Related posts

1 comment

  1. Orders are custom post types of shop_order type. Orders do not have thumbnails themselves, but orders have a list of products that were purchased and each product has the possibility of a thumbnail.

    You can see in the order-details.php template how to get all the items/products associated with any order object… $order->get_items()

    This returns an array of data that is stored in separate database tables. With the $item variable you can get the original product and you can see in the linked template that the $product variable that is being sent to the order-details-item.php is defined as order->get_product_from_item( $item ).

    Anyway, once you have the $product object you can use $product->get_image() to retrieve the product’s image.

    As a simplified example, this would show the thumbnails for all the products purchased in order 999.

    $order_id = 999;
    $order = wc_get_order( $order_id );
    foreach( $order->get_items() as $item_id => $item ) {
            $product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
            echo $product->get_image();
    }
    

    Nesting this inside of your loop:

    foreach ( $customer_orders as $customer_order ) { 
       $order = wc_get_order(); 
       $order->populate( $customer_order ); 
       foreach( $order->get_items() as $item_id => $item ) {
                $product' = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
                echo $product->get_image();
        }
    }
    

    Though normally, the order-details.php template should have links to an overview of each individual order.

Comments are closed.