php - Add product thumbnails to Woocommerce admin orders list, only if product still exists - Stack Overflow

I use Add products thumbnail to Woocommerce admin orders list answer code. It works fine, but recently

I use Add products thumbnail to Woocommerce admin orders list answer code. It works fine, but recently I deleted some products, so they do not exist anymore and this causes an error. How to avoid an error if a purchased product has been removed?

I guess I need to check the product additionally, and if it doesn't exist, ignore it/leave empty and move to the next product. Any ideas how can it be reached?

I use Add products thumbnail to Woocommerce admin orders list answer code. It works fine, but recently I deleted some products, so they do not exist anymore and this causes an error. How to avoid an error if a purchased product has been removed?

I guess I need to check the product additionally, and if it doesn't exist, ignore it/leave empty and move to the next product. Any ideas how can it be reached?

Share Improve this question edited Nov 21, 2024 at 8:30 LoicTheAztec 255k24 gold badges397 silver badges443 bronze badges asked Nov 20, 2024 at 16:23 user21098450user21098450 193 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can check if product exists in your code by simply testing $product as the $item->get_product() will return false on pemanently deleted products.

// 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;"' : '';
            if ( $product ) {
              // Display product thumbnail
                printf( '<span%s>%s</span>', $style, $product->get_image( array( 50, 50 ) ) );    
                $count++;
            }
        }
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742345031a4426373.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信