【问题标题】:Add products thumbnail to Woocommerce admin orders list将产品缩略图添加到 Woocommerce 管理员订单列表
【发布时间】:2019-05-12 14:41:27
【问题描述】:

我想在 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 );
    }
}

【问题讨论】:

    标签: php wordpress woocommerce backend orders


    【解决方案1】:

    请注意,因为订单可以有许多产品(许多订单项目),在这种情况下,您将有许多图像(它也会压低页面)...

    现在你的第二个函数钩子是错误的,不会做任何事情。

    因此,您需要按如下方式遍历订单项:

    // 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 文件中。经过测试并且可以工作。

    【讨论】:

    • 你让我很开心,非常非常感谢
    • 如何使它扩展 wc-api 以便我们可以在移动应用程序的订单页面中显示图像?谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 2020-05-20
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2016-07-26
    相关资源
    最近更新 更多