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?
- Outdated Order Object Initialization: Using
new WC_Order($order_id)is legacy WooCommerce syntax (pre-3.0). Modern WooCommerce recommendswc_get_order()for reliable order data retrieval. - Direct Property Access: Accessing
$order->billing_first_namedirectly works but isn't the recommended way—getter methods are more future-proof. - 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'sget_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_newis 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_newappears 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();(orupdate_post_meta($order_id, 'public_details_new', $user_input)for older setups).
内容的提问来源于stack exchange,提问作者Alim Jordan
相关产品推荐
相关产品推荐

