将产品图片添加到 Woocommerce 我的帐户订单视图 [英] Add the product image to Woocommerce my account order view
问题描述
我想在 Woocommerce 的帐户视图订单页面上添加图片.有些我可以获取图片,但尺寸太大,我不知道我需要在哪里添加此代码:
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();
}
?>
任何帮助将不胜感激
这是查看订单页面上的位置,我想在此处添加产品图片:
Here is the location on the View order pages, wher I would like to add the product image:
推荐答案
以下挂钩函数将完成这项工作(您可能需要添加一些 CSS 样式规则):
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;
}
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
注意WC_Product
方法 get_image()
在内部使用 Wordpress get_the_post_thumbnail()
.
更新:在代码中添加了if( $product->get_image_id() > 0 )
,以显示产品图片,仅当它存在时.
Update: added
if( $product->get_image_id() > 0 )
to the code, to display the product image, only when it exist.
这篇关于将产品图片添加到 Woocommerce 我的帐户订单视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!