admin管理员组文章数量:1334798
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++;
}
}
}
}
本文标签:
版权声明:本文标题:php - Add product thumbnails to Woocommerce admin orders list, only if product still exists - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742345031a2457350.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论