且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-29 21:39:40

请注意,因为订单可能有多个产品(多个订单项),并且在此CA中您将有多个图像(也会拖累页面)

现在您的第二个函数挂钩错误,不会执行任何操作。

至,因此您需要按如下方式循环查看订单项目:

// 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文件中。已测试并正常工作。