将产品图片添加到 Woocommerce 我的帐户订单视图

Add the product image to Woocommerce my account order view

我想在 Woocommerce.some 中的我的帐户查看订单页面上添加图片 我可以获取图片,但尺寸太大,我不知道我需要在哪里添加此代码:

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

    }
?>   

任何帮助将不胜感激

这是查看订单页面上我想添加产品图片的位置:

无需获取主图,只需通过 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();

    }
?>   

下面的钩子函数将完成这项工作(您可能需要添加一些 CSS 样式规则):

// 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;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

注意 WC_Product method get_image() 在内部使用 Wordpress get_the_post_thumbnail()


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

写在woocommerce/template/order/ordr-details-item.php

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

这将为您提供一个 80x80 尺寸的图像,您可以根据需要进行更改。

我知道这个主题更多的是关于产品图片,但是我如何上传产品变体的客户特定徽标,因为他还没有准备好上传。我用产品页面的功能和逻辑做的上传和预览。但是现在我缺少更新不完整订单的部分。 我试过这段代码:

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);};

);
      };
   }
}

此代码与 add_action 位于同一文件中,用于在购物车上添加徽标作为产品变体图片上传。