【发布时间】:2021-12-19 18:15:34
【问题描述】:
由于 WooCommerce 不在订单页面上显示产品图片,我创建了一个 PHP 函数来添加新列并显示我需要的所有详细信息,但我遇到了一些问题。
当旧订单中的一个产品被删除时,订单出现错误,因为该图像/产品不存在,并返回严重错误。
有人可以给我这个案例的可能解决方案吗?
我需要告诉 wordpress“如果此图像/产品不存在或为空,它会显示一些文本”
// The data of the new custom column in admin order list
add_action( 'manage_shop_order_posts_custom_column' , 'admin_orders_list_column_content', 1, 2 );
function admin_orders_list_column_content( $column, $post_id ){
global $the_order, $post;
if ('custom_column' === $column) {
// Start list
echo '<ul class="orders-list-items-preview">';
// Loop through order items
foreach($the_order->get_items() as $item) {
$product = $item->get_product();
$img = wp_get_attachment_url($product->get_image_id());
if(file_exists($img)){
$img = wp_get_attachment_url($product->get_image_id());
}else{
echo 'texto2';
}
$name = $item->get_name();
$qty = $item->get_quantity();
echo "<li>
<img src=\"$img\" />
<label>$qty</label> $name
</li>";
}
// End list
echo '</ul>';
}
}
【问题讨论】:
标签: php wordpress woocommerce backend orders