3

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

Community
  • 1
  • 1
Parth Shah
  • 167
  • 3
  • 14

4 Answers4

10

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi @Loic TheAztec it help me but if product image is not available in some products so what can i write else condition because when i click on order history the page reloads and nothing it seems its whole blank your help will be appriciated – Parth Shah Aug 14 '18 at 16:20
  • @ParthShah Sorry I just tested that now, it works without any problem like you describe. My updated code, display the item image only if it exist. If there is no image set for the product, it keep the normal behavior of woocommerce. So there is something else that you have done, another customization, something in your theme or a plugin that is making trouble. – LoicTheAztec Aug 14 '18 at 17:06
  • Can u help me as i have uploaded that order from csv so that product are not in product category so i think it wont get any product id as in database product id is null so what could be condition for null ... – Parth Shah Aug 14 '18 at 17:49
  • @ParthShah Sorry, but you should better ask a new question, as I really don't catch your issue. – LoicTheAztec Aug 14 '18 at 18:45
  • @LoicTheAztec I added your code in function.php and I am getting the image in the purchase list instead of the order list. – user9437856 Feb 11 '21 at 08:18
  • @user9437856 **It is not for order list,** read the question… Yes it still works in WooCommerce 4.9.2 on My Account > orders > **single view order**… If it doesn't is because something else it's making trouble, so it can be your theme, a plugin or some lther code added by you. – LoicTheAztec Feb 11 '21 at 08:18
  • 1
    @LoicTheAztec, Yes, I deleted my first comment. I just added the code in the function and checked my order page. It was not displaying so I thought some issue with the code. Then I click on the view button and there I got an image. – user9437856 Feb 11 '21 at 08:24
  • @LoicTheAztec, I ask the new question for displaying the image on the order page https://stackoverflow.com/questions/66151448/what-hook-i-have-to-use-to-display-the-product-image-on-order-page – user9437856 Feb 11 '21 at 08:50
0

Instead of getting the main image, you just need to fetch the thumbnail via get_the_post_thumbnail():

<?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 get_the_post_thumbnail();

    }
?>   
0

Write it on woocommerce/template/order/ordr-details-item.php

echo '<div class="product-imgae">'.$product->get_image(array( 80, 80)).'</div>';

this will give you a 80x80 size image change it according to your neesd.

Kalo Heem
  • 79
  • 1
  • 3
0

I know this topic is more for the Product Image, but how I can upload the Customer Specific Logo for a Product variant as he not had it ready for upload. The Upload and Preview I made with the Function and Logic of the Product Page. But now I'm missing the part to update the order that not are complete. I did try with this code:

add_filter('woocommerce_order_item_meta_end', '_hook_order_detail_item', 30, 3);
function _hook_order_detail_item($item_id, $item, $order)
{
   foreach ($order->get_items() as $inner_item)
   {
      if ($inner_item['_img_file_degree']&&$item_id===$inner_item->get_id())
      {
          $_img_file = $inner_item['_img_file'];
          $_img_file_title = $_img_file[0]['title'];
          //print_r($_img_file_title);
          //$order_id = $order->get_order_number();$cart_items = $order->get_items( apply_filters( 'woocommerce_purchase_order_item_types', 'line_item' ) );
          //echo '<pre>Hallo<br />'; print_r($cart_items);echo '<br /></pre>';
         echo '<ul class="wc-item-meta" style="list-style: none; padding-left: 0; font-size: inherit;"><li><strong class="wc-item-meta-label">' . esc_html__('Bild hochgeladen:', 'woocommerce') . '</strong><span>' . $_img_file_title . '</span></li><li><strong class="wc-item-meta-label">' . esc_html__("Logo-Bilddrehung:", "woocommerce") . '</strong><span>' . $inner_item['_img_file_degree'] . '</span></li></ul>';
      }
      elseif (is_wc_endpoint_url( 'view-order' ))
      {
          $order_id = $order->get_order_number();$cart_items = $order->get_items( apply_filters( 'woocommerce_purchase_order_item_types', 'line_item' ) );
          $items = $order->get_items();
          foreach ( $items as $item ) {
              $product_name = $item->get_name();
              $product_id = $item->get_product_id();
              $product_variation_id = $item->get_variation_id();
          };
          ?><form class="variations_form" method="post"><?php display_additional_product_fields();
          // echo '<pre>Hallo<br />'; print_r($product_variation_id);echo '<br /></pre>';
          ?><button type="submit" class="button" name="update_product" id="image_variant_upload" value="<?php esc_attr_e( 'Update product', 'woocommerce' ); ?>"><?php esc_html_e( 'Update product', 'woocommerce' ); ?>
          </button>
          <input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
          <input type="hidden" name="variation_id" class="variation_id" value="<?php echo $product_variation_id; ?>">
          </form><?php
          if(array_key_exists('update_product', $_POST)) {add_custom_fields_data_as_custom_cart_item_data($cart_item, $product_id);};

);
      };
   }
}

This Code is in the same file like the add_action to add the Logo on Cart as Product Variation Image upload.

  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/30446777) – bitski Nov 27 '21 at 19:28