【问题标题】:Issue with displaying product image on WooCommerce admin orders list在 WooCommerce 管理员订单列表上显示产品图片的问题
【发布时间】: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


    【解决方案1】:

    wp_get_attachment_url() 将返回带有附件 URL 的字符串,否则为 false。那么您能否通过 if 条件添加额外的检查。

    然后你得到:

    function action_manage_shop_order_posts_custom_column( $column, $post_id ) {    
        // Compare
        if ( $column == 'custom_column' ) {
            // Get order
            $order = wc_get_order( $post_id );
            
            // Is a WC_Order
            if ( is_a( $order, 'WC_Order' ) ) {
                // Start list
                echo '<ul class="orders-list-items-preview">';
                
                // Loop through order items
                foreach ( $order->get_items() as $item_key => $item ) {
                    // Get product
                    $product = $item->get_product();
                    
                    // Is a WC product
                    if ( is_a( $product, 'WC_Product' ) ) {         
                        // Getters
                        $image_id   = $product->get_image_id();
                        $image      = wp_get_attachment_url( $image_id );
                        $name       = $item->get_name();
                        $qty        = $item->get_quantity();
                        
                        // NOT false
                        if ( $image ) {
                            $image_output = '<img src=' . $image . ' width="50" height="50">';
                        } else {
                            $image_output = __( 'Some text', 'woocommerce' );
                        }
                        
                        // Output
                        echo '<li>' . $image_output . '<label>' . $qty . '</label>' . $name . '</li>';
                    } else {
                        // Output
                        echo '<li>' . __( 'N/A', 'woocommerce' ) . '</li>';
                    }
                }
                
                // End list
                echo '</ul>';
            }
        }
    }
    add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );
    

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 2018-08-08
      • 2021-06-25
      • 2021-05-14
      • 2022-01-15
      相关资源
      最近更新 更多