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

WooCommerce发票插件自定义字段加载异常求助

Fix for Empty Custom Field in WooCommerce Print Invoices & Packing Lists

Looks like your custom field public_details_new is showing up empty because of a few outdated practices and missing checks in your code. Let's walk through the fixes step by step.

What's Causing the Issue?

  1. Outdated Order Object Initialization: Using new WC_Order($order_id) is legacy WooCommerce syntax (pre-3.0). Modern WooCommerce recommends wc_get_order() for reliable order data retrieval.
  2. Direct Property Access: Accessing $order->billing_first_name directly works but isn't the recommended way—getter methods are more future-proof.
  3. Unreliable Meta Data Fetch: get_post_meta() works for post types, but orders in WooCommerce 3.0+ have their own meta handling, so using the order object's get_meta() method is more reliable.

Corrected Code

add_action('wc_pip_after_body', 'dvsi_invoice_footer');
function dvsi_invoice_footer($order_id) {
    // Get the order object using modern WooCommerce function
    $order = wc_get_order($order_id);
    
    // Bail if the order doesn't exist to avoid errors
    if (!$order) return;

    // Use the official getter method for billing first name
    echo '<h4>'.__('Name: ').'</strong> ' . $order->get_billing_first_name() . '</h4>';
    
    // Fetch the custom meta using the order object's method
    $public_details = $order->get_meta('public_details_new', true);
    // Fallback text if the field is empty
    echo '<h4>'.__('Note: ').'</strong> ' . (!empty($public_details) ? $public_details : 'No note provided') . '</h4>';
?>
<h4 style="margin-bottom:0px;">Important:</h4>
<p>If you have purchased Domain, Hosting, SSL certificate or any other service that has an Expiry date then please contact us 10 days before prior to the Expiry date so we can renew or repurchase it on time.</p>
<h4 style="margin-bottom:0px;">Contact Details</h4>
<p><strong>Office: </strong>AL-Hafeez Heights Gulberg III, Lahore</p>
<p><strong>Mobile: </strong>0323-4152099</p>
<p><strong>Email: </strong>contact@devsol.pk</p>
<?php
}

Key Improvements Explained

  • wc_get_order(): This function ensures you're getting a valid, up-to-date order object compatible with the latest WooCommerce versions.
  • Order Existence Check: Prevents PHP errors if the hook is triggered with an invalid or missing order ID.
  • get_billing_first_name(): Official getter method for billing data—this will work even if WooCommerce changes how it stores order data in future updates.
  • get_meta(): Designed specifically for WooCommerce order meta, it handles the relationship between orders and their post data automatically. We also added a fallback message so empty fields don't look broken.

Additional Troubleshooting Steps

If the custom field is still empty after updating the code:

  • Verify the Field Name: Double-check that public_details_new is exactly the same as the meta key used when saving the field (meta keys are case-sensitive).
  • Check if the Meta is Saved: Go to the order edit page in your WordPress admin, enable "Custom Fields" via the Screen Options dropdown, and confirm public_details_new appears with a value.
  • Confirm Saving Logic: If this field comes from a frontend form, make sure you're saving it correctly using $order->update_meta_data('public_details_new', $user_input); followed by $order->save(); (or update_post_meta($order_id, 'public_details_new', $user_input) for older setups).

内容的提问来源于stack exchange,提问作者Alim Jordan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:24:33