WooCommerce结账自定义字段导出至ShipStation自定义字段2失败求助
Let's break down why your data isn't syncing and fix it step by step.
The Root Issue
Looking at your code, there's a critical mismatch between the meta key you're saving to the order and the one you're telling ShipStation to pull.
When you save the checkout field data, you're using:
update_post_meta( $order_id, 'Out of Stock Option', sanitize_text_field( $_POST['outofstockoption'] ) );
This stores the value under the meta key Out of Stock Option (with spaces and capitalization). But in your ShipStation filter, you're returning outofstockoption (all lowercase, no spaces). WordPress post meta keys are case-sensitive, so these are treated as two completely different keys—ShipStation can't find the data because it's looking for the wrong key.
Step 1: Fix the Order Meta Saving
First, update your checkout field save function to use a consistent, standard meta key (lowercase, no spaces is best practice for WordPress meta keys):
function my_custom_checkout_field_update_order_meta( $order_id ) { if ( ! empty( $_POST['outofstockoption'] ) ) { // Use a consistent, lowercase meta key without spaces update_post_meta( $order_id, 'outofstockoption', sanitize_text_field( $_POST['outofstockoption'] ) ); // Optional: If you want this field to show in the WooCommerce order admin edit screen, add this line too add_post_meta( $order_id, '_outofstockoption', sanitize_text_field( $_POST['outofstockoption'] ), true ); } } add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
Step 2: Update the ShipStation Filter
Next, adjust your ShipStation filter to use the same meta key, and make it more robust by using the $order object parameter (the filter passes the order object as a second argument, which is more reliable than just returning a key):
add_filter( 'woocommerce_shipstation_export_custom_field_2', 'shipstation_map_outofstock_option_to_custom_field_2', 10, 2 ); function shipstation_map_outofstock_option_to_custom_field_2( $value, $order ) { // Pull the data directly from the order object using the correct meta key $outofstock_option = $order->get_meta( 'outofstockoption', true ); // Return the custom field value if it exists, otherwise keep the original value return ! empty( $outofstock_option ) ? $outofstock_option : $value; }
Additional Checks to Ensure Sync Works
- Verify the Checkout Field Name: Make sure your checkout field's HTML
nameattribute is exactlyoutofstockoption(matches the$_POSTkey in your save function). - Test with a New Order: Old orders won't have the corrected meta data—create a test order with the checkout field filled out to test the sync.
- Confirm Meta Data in WooCommerce: Go to the order edit screen in WooCommerce, scroll to the "Custom Fields" section, and check that
outofstockoptionis present with the correct value. - Re-sync Orders in ShipStation: If you're testing with existing orders, trigger a manual re-sync in ShipStation to pull the updated meta data.
内容的提问来源于stack exchange,提问作者Michael Cardeno

