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 badges1 Answer
Reset to default 1You 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条)