将产品缩略图添加到WooCommerce管理订单列表 [英] Add products thumbnail to Woocommerce admin orders list

查看:69
本文介绍了将产品缩略图添加到WooCommerce管理订单列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

请注意,因为订单可能有多个产品(多个订单项),并且在此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文件中。已测试并正常工作。

这篇关于将产品缩略图添加到WooCommerce管理订单列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆