自定义插件在编辑订单页调用wc_get_order时页面崩溃求助
Hey Jen, let's dig into why your WooCommerce order edit page is crashing when using wc_get_order() in your custom EasyPost plugin. I've run into similar issues before, so here are some targeted fixes and troubleshooting steps to get you back on track:
The most common culprit here is an invalid or undefined value being passed to wc_get_order(). Your code snippet cuts off at $o..., so let's validate that parameter first:
- Add debug checks right before calling the function to see what you're working with:
// Log what $o contains to debug.log error_log( 'Order parameter being passed: ' . print_r( $o, true ) ); // Exit early if the value is invalid if ( empty( $o ) || ! is_numeric( $o ) ) { error_log( 'Invalid order ID provided to wc_get_order()' ); return; } $order = wc_get_order( $o );
- On the WooCommerce order edit page, the valid order ID is usually available via
$_GET['post'](since orders are a custom post type). You can grab it directly like this:
$order_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; if ( $order_id ) { $order = wc_get_order( $order_id ); } else { // Handle the case where no order ID is present echo '<p>Could not load order details. Please ensure you are on a valid order edit page.</p>'; return; }
If your plugin executes before WooCommerce initializes, wc_get_order() won't exist yet—this causes a fatal error and crashes the page. Fix this by hooking your meta box code to a WooCommerce-specific action:
// Hook into WooCommerce's init action to ensure functions are available add_action( 'woocommerce_init', 'jens_easypost_register_meta_box' ); function jens_easypost_register_meta_box() { add_meta_box( 'jens-easypost-integration', 'EasyPost Shipping Tools', 'jens_easypost_meta_box_content', 'shop_order', 'normal', 'high' ); } function jens_easypost_meta_box_content( $post ) { // Use the post ID from the meta box context (this is the order ID) $order = wc_get_order( $post->ID ); if ( ! $order ) { error_log( 'Failed to load order with ID: ' . $post->ID ); echo '<p>Error loading order details.</p>'; return; } // Your EasyPost integration code here }
Page crashes almost always come with a hidden fatal error. Turn on debug mode to reveal what's going wrong:
- Open your
wp-config.phpfile and update these lines:
define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', true );
- Refresh the order edit page—you'll see the specific error message (e.g., "Call to undefined function wc_get_order()" or "Invalid argument supplied"). If it's not displayed, check
wp-content/debug.logfor details.
Other plugins or your active theme might be overriding WooCommerce order functions, causing conflicts:
- Temporarily disable all plugins except WooCommerce and your custom EasyPost plugin. If the page works, re-enable plugins one by one to find the conflicting one.
- Switch to a default WordPress theme (like Twenty Twenty-Four) to confirm your theme isn't interfering.
wc_get_order() has evolved across WooCommerce versions. If you're using an older WooCommerce release, you might need to fall back to the legacy constructor:
// For WooCommerce versions < 3.0 (though you should update if possible) $order = new WC_Order( $order_id );
内容的提问来源于stack exchange,提问作者Jen Born

