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

Par*_*hah 2 wordpress image orders woocommerce hook-woocommerce

我想在Woocommerce的帐户查看订单页面上添加图片。有人可以获取图片,但是尺寸太大,我不知道该在哪里添加此代码:

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

    }
?>   
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激

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

查看订单

Loi*_*tec 5

以下钩子函数将完成此工作(您可能需要添加一些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;
}
Run Code Online (Sandbox Code Playgroud)

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。

在此处输入图片说明

请注意,WC_Product方法在get_image()get_the_post_thumbnail()内部使用Wordpress 。


更新:添加if( $product->get_image_id() > 0 )到代码中,仅在存在时显示产品图像。