I Got a WooCommerce store with the Shipstation for WooCommerce plugin installed. They provide a simple PHP function to send custom field data associated with an order to ShipStation. Here's the snippet:
add_filter( 'woocommerce_shipstation_export_custom_field_2', 'shipstation_custom_field_2' );
function shipstation_custom_field_2() {
return '_meta_key'; // Replace this with the key of your custom field
}
This works great as long as the _meta_key you want to set is located in the wp_postmeta table. But the meta key is located in wp_woocommerce_order_itemmeta table (the related meta key is _wc_checkout_add_on_value).
Is there some way to get the ShipStation function above to look up _wc_checkout_add_on_value in the wp_woodcommerce_order_itemmeta table?
Or, as a workaround, is there a way to return a fixed text string instead of a meta key? I've tried everything I can think of but can't get it to work. Seems to want a meta key from the wp_postmeta table and nothing else will do.
Here's the logic I think might work here:
- Check for a particular value in the
wp_woocommerce_order_itemmetatable for each order - If the value is found, send a fixed text string for the "shipstation_custom_field_2" that gets send to ShipStation.
I can do the first part of this. It's the second part, sending a fixed value to SS, that I can't seem to get working. Maybe it's impossible with this function.
Any ideas or workarounds would be most welcome.
add_filter( 'woocommerce_shipstation_export_custom_field_2', 'shipstation_custom_field_2' );
function shipstation_custom_field_2() {
//return '_meta_key'; // Replace this with the key of your custom field
//return 'test';
echo 'test';
}
Updated: What you can try to do when the order is just created, is to check order items for this custom order item metadata and add it as order metadata.
But if there are multiple items (at least 2) you have 2 choices:
So there are 2 cases:
1). Set the data as a comma separated string (for one or multiple items)
Code goes in functions.php file of your child theme (or in a plugin).
You can change the separator
', 'as you like in the implode() function.If there is only one order item, you will get the value just like in the order item.
2). Set the data as an array of values (for one or multiple items)
Simply replace in my function:
with:
To finish:
You will use your ShipStation filter hook as follows: