I'm hooking into action 'woocommerce_order_status_completed' with a priority of 9999 and even then, the custom fields I have added to the order are not yet in the postmeta table. In my function, I'm just dumping to error log the postmeta fields, and the custom fields I added to the order page and not in the postmeta table (though when I check in phpmyadmin they're there.) What priority # should I use to execute after Toolset has added the fields to postmeta?
Hi Catherine,
Thank you for waiting, while I performed a few tests on my own website.
The way "woocommerce_order_status_completed" hook works it only gets triggered when an order's status is changed from "any other status" to "Completed".
But when an admin creates a new order from the admin area and sets "Completed" status directly, this hook ( woocommerce_order_status_completed ) is not triggered at that time.
This hook works if you've set a different status to the order while creating it and then that order is saved again, setting the order status to "Completed".
I hope this clarifies that this limitation comes from WooCommerce's own hook and not from WordPress or any other plugin like Toolset.
A simple workaround to make this code work is to make sure that a new order is created from the admin area, with initial status set to a different status like "Processing" and then setting it to "Completed".
Alternatively, you'll need to create a new function specifically for execution at the time of new order post's creation, using hooks like:
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_insert_post
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
regards,
Waqar
Hi Waqar,
Thanks for your response and assistance. The 'woocommerce_order_status_completed' does get fired on a new order. From my sample code I showed, I am dumping the order postmeta to the error log on that action, and it does get outputted when I enter a new order.
My method was working when I was using ACF plugin for custom fields, so I guess they hook in at a different point from Toolset to add custom fields. I'll try moving my logic to the save_post action and see if that works.
Thanks!
My issue is resolved now. Thank you!