You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义WooCommerce邮件:产品缩略图与价格获取异常排查

Troubleshooting Your WooCommerce Custom Email Issue

Hey Jamie, let's break down why adding the product image logic broke your price retrieval and order processing—this is a common pitfall with small syntax oversights and WooCommerce item handling!

1. Immediate Fix: Missing Semicolon (Critical Syntax Error)

Looking at your code, the line where you fetch the product image is missing a trailing semicolon:

$prodimage = $product->get_image( array( 200, 200 ) ) // Missing ; here!

PHP stops parsing code as soon as it hits this syntax error, which means everything after this line (including your $product_price assignment) never runs. This explains why commenting out the price-related code (or indirectly bypassing the broken image line) restored functionality.

Fix it by adding the semicolon:

$prodimage = $product->get_image( array( 200, 200 ) );

2. Add Robustness Checks for Product Existence

Even with the semicolon fixed, if wc_get_product( $product_id ) returns false (e.g., if the product was deleted after the order was placed), calling get_image() or get_price() will throw a fatal error. Add a check to handle this edge case:

$product_id = $theitem['product_id'];
$product = wc_get_product( $product_id );

// Only proceed if the product exists
if ( $product ) {
    $prodimage = $product->get_image( array( 200, 200 ) );
    $product_price = $product->get_price();
    // Rest of your product-related logic here
} else {
    // Fallback for missing products (optional)
    $prodimage = '';
    $product_price = 'N/A';
}

3. Handle Variable Products Correctly

If your store uses variable products, $theitem['product_id'] gives you the parent product ID, not the specific variation the customer ordered. To get the correct variation price and image, use the variation ID instead:

// Get variation ID if available, else use product ID
$product_id = $theitem->get_variation_id() ? $theitem->get_variation_id() : $theitem['product_id'];
$product = wc_get_product( $product_id );

This ensures you're pulling data from the exact variation the customer purchased, not the parent product which might not have a set price.

Full Corrected Code Snippet

Here's your code with all the fixes applied:

<?php foreach ($order->get_items() as $theitem_id => $theitem ) { 
    // PRODUCT NAME
    $product_name = $theitem->get_name();
    // PRODUCT QUANTITY
    $quantity = $theitem->get_quantity();
    // LINE ITEM SUBTOTAL (Non discounted)
    $item_subtotal = $theitem->get_subtotal();
    $item_subtotal = number_format( $item_subtotal, 2 );
    // LINE ITEM TOTAL (discounted)
    $item_total = $theitem->get_total();
    $item_total = number_format( $item_total, 2 );

    // Get correct product/variation ID
    $product_id = $theitem->get_variation_id() ? $theitem->get_variation_id() : $theitem['product_id'];
    $product = wc_get_product( $product_id );

    // PRODUCT IMAGE & PRICE (with existence check)
    if ( $product ) {
        $prodimage = $product->get_image( array( 200, 200 ) );
        $product_price = $product->get_price();
        // Format price for consistency
        $product_price = number_format( (float)$product_price, 2 );
    } else {
        $prodimage = '';
        $product_price = 'N/A';
    }
?>

Start with fixing the missing semicolon first—this is almost certainly the root cause of your immediate issue. The other checks will help prevent future errors with missing products or variable items.

内容的提问来源于stack exchange,提问作者Jamie Clague

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:45:49