为什么我的产品图片导致 WooCommerce 结账时出错?

Why is my product image causing an error on WooCommerce checkout?

我有这个 return 订单状态代码,我还想 return 产品图片(如果购买了多个产品,则为图片)方式(即 echo 'product image')。

代码:

add_action( 'woocommerce_before_thankyou', 'custom_content_thankyou', 10, 1 );

function custom_content_thankyou($order_id) {
    if ( ! $order_id )
        return;

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Get the order key
    $order_key = $order->get_order_key();

    // Get the order number
    $order_key = $order->get_order_number();

    if($order->is_paid())
        $paid = __('yes');
    else
        $paid = __('no');

    // Loop through order items
    foreach ( $order->get_items() as $item_id => $item ) {

        // Get the product object
        $product = $item->get_product();

        // Get the product Id
        $product_id = $product->get_id();
        
        /* Get product thumbnail */
        $thumbnail = $_product->get_image(); // THIS IS THE LINE CAUSING THE ERROR

        // Get the product name
        $product_id = $item->get_name();
    
    }

    // Output some data
    echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
   // echo 'product image';

}

除了这一行,我上面的代码有效:

$thumbnail = $_product->get_image();

导致此错误的原因:

致命错误:未捕获错误:在 /home/customer/www/example.com/public_html/wp-content/themes/kalium-child/functions.php:208 中的 null 上调用成员函数 get_image()堆栈跟踪:#0 /home/customer/www/example.com/public_html/wp-includes/class-wp-hook.php(307): custom_content_thankyou(1313) #1 /home/customer/www/example.com/public_html/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters('', 数组) #2 /home/customer/www/example.com/public_html/wp-includes/plugin.php(474): WP_Hook->do_action(数组)#3 /home/customer/www/example.com/public_html/wp-content/themes/kalium-child/woocommerce/checkout/thankyou.php(26): do_action('woocommerce_bef...', 1313) #4 /home/customer/www/example.com/public_html/wp-content/plugins/woocommerce/includes/wc-core-functions.php(345): include('/home/customer/...') #5 /home/customer/www/example.com/public_html/wp-content/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-checkout.php(256): wc_get_template('checkout/thanky...', Array) #6 /home/customer/w in /home/customer/www/example.com/public_html/wp-content/themes/kalium-child/functions.php on 208行 此网站出现严重错误。

我确定我在这里遗漏了一些明显的东西,但我无法解决。有什么想法吗?

它返回 null 因为它在 $_product.

中没有找到任何东西

$product 在没有下划线的循环中抓取产品。去掉下划线应该就可以了

$thumbnail = $product->get_image();