简体   繁体   English

将产品缩略图添加到 Woocommerce 管理订单列表

[英]Add products thumbnail to Woocommerce admin orders list

I would like to add futured image on admin view order pages in Woocommerce.我想在 Woocommerce 的管理视图订单页面上添加未来图像。 New Column created, but the product image does not appear.已创建新列,但未显示产品图像。 What should I do to show the order thumbnail?我应该怎么做才能显示订单缩略图? Thanks.谢谢。

// Admin Order page new colums
add_filter( 'manage_edit-shop_order_columns', 'add_account_orders_column', 10, 1 );
function add_account_orders_column( $columns ){
    $columns['custom-column'] = __( 'New Column', 'woocommerce' );

    return $columns;
}

add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'add_account_orders_column_rows' );
function add_account_orders_column_rows( $order ) {
    // Example with a custom field
    if ( $value = $order->get_meta( 'order_received_item_thumbnail_image' ) ) {
        echo esc_html( $value );
    }
}

Beware, as orders can have many products (many order items) and in this cas you will have many images (also it will weigh down the page)当心,因为订单可以有很多产品(很多订单项目),在这个 cas 你会有很多图片(它也会压低页面) ......

Now Your 2nd function hook is wrong and will not do anything.现在你的第二个函数钩子是错误的,不会做任何事情。

To so you need to loop through order items as follow:为此,您需要按如下方式遍历订单项:

// Add a new custom column to admin order list
add_filter( 'manage_edit-shop_order_columns', 'admin_orders_list_add_column', 10, 1 );
function admin_orders_list_add_column( $columns ){
    $columns['custom_column'] = __( 'New Column', 'woocommerce' );

    return $columns;
}

// The data of the new custom column in admin order list
add_action( 'manage_shop_order_posts_custom_column' , 'admin_orders_list_column_content', 10, 2 );
function admin_orders_list_column_content( $column, $post_id ){
    global $the_order;

    if( 'custom_column' === $column ){
        $count = 0;

        // Loop through order items
        foreach( $the_order->get_items() as $item ) {
            $product = $item->get_product(); // The WC_Product Object
            $style   = $count > 0 ? ' style="padding-left:6px;"' : '';

            // Display product thumbnail
            printf( '<span%s>%s</span>', $style, $product->get_image( array( 50, 50 ) ) );

            $count++;
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM