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?
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++;
}
}
}
}