dongxia8656 2019-05-12 14:41
浏览 120
已采纳

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

I would like to add futured image on admin view order pages in 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 );
    }
}
  • 写回答

1条回答 默认 最新

  • douhui3760 2019-05-12 17:10
    关注

    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)

    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). Tested and works.

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题