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

3
我想在Woocommerce的管理员查看订单页面上添加未来图片。已创建新列,但产品图片未显示。 我该怎么做才能显示订单缩略图? 谢谢。
// 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 );
    }
}
1个回答

7

请注意,一个订单可能包含多个产品(多个订单项),在这种情况下,您将有许多图片(也会拖慢页面加载速度)...

现在,您的第二个功能挂钩是错误的,并且不会起作用。

为此,您需要按以下方式循环遍历订单项:

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

将代码放在活动子主题(或活动主题)的functions.php文件中。已测试并且有效。

在此输入图片描述


1
你让我非常开心,非常非常感谢你。 - gokturk_mg
如何扩展wc-api,以便我们可以在移动应用程序的订单页面中显示图像?谢谢! - ZeroCode

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接