Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add the product image to Woocommerce my account order view

I would like to add image on my account view order pages in Woocommerce.some one I am able to get the image but the size is too large and I don't know where I need to add this code:

<?php 
    // Get a list of all items that belong to the order
    $products = $order->get_items();

    // Loop through the items and get the product image
    foreach( $products as $product ) {                  

        $product_obj = new WC_Product( $product["product_id"] );

        echo $product_obj->get_image();

    }
?>   

Any help will be appreciated

Here is the location on the View order pages, wher I would like to add the product image:

view order

like image 367
Parth Shah Avatar asked Dec 18 '25 23:12

Parth Shah


1 Answers

The following hooked function will do the job (You may need to add some CSS style rules):

// Display the product thumbnail in order view pages
add_filter( 'woocommerce_order_item_name', 'display_product_image_in_order_item', 20, 3 );
function display_product_image_in_order_item( $item_name, $item, $is_visible ) {
    // Targeting view order pages only
    if( is_wc_endpoint_url( 'view-order' ) ) {
        $product   = $item->get_product(); // Get the WC_Product object (from order item)
        $thumbnail = $product->get_image(array( 36, 36)); // Get the product thumbnail (from product object)
        if( $product->get_image_id() > 0 )
            $item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>' . $item_name;
    }
    return $item_name;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

Note the WC_Product method get_image() uses Wordpress get_the_post_thumbnail() internally.


Update: added if( $product->get_image_id() > 0 ) to the code, to display the product image, only when it exist.

like image 76
LoicTheAztec Avatar answered Dec 20 '25 15:12

LoicTheAztec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!