Alterar campo meta na página de pedido no admin
-
Estou usando o ACF para ter alguns campos extras em meu pedido que s?o preenchidos automaticamente via programa??o quando o status muda para “Processando”. Para isso estou usando woocommerce_order_status_changed. Esta funcionando normal exceto quando a altera??o do status é feita manualmente na pagina do admmin.
Alguém sabe me dizer o pro que isso ocorre?
-
Why not use
woocommerce_order_status_processing
as the hook?Hello Thomas.
I do not use this hook because for each status I have a different action, so I have a conditional to check what the current status isThanks
@felipe152 – I just tested this locally and it indeed works for me. I tested by just creating a small logging function that is hooked to
woocommerce_order_status_changed
. Even if I changed the status manually it fired the logging function, so this should work.Have you tested with a default theme and only WooCommerce active?
We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.
Sorry but did not solve.
Thomas, I think your test was not adequate. The problem is when I use this hook (woocommerce_order_status_changed) to change an order field that is being updated by the admin panelIf I use woocommerce_order_status_processing, can I get the order id?
@felipe152 – what about using the transition hooks since you want to check them conditionally anyways?
For instance,
woocommerce_order_status_pending_to_processing
fires when the order goes from Pending Payment to Processing. The hook can do the conditional checking for you.https://hookr.io/actions/woocommerce_order_status_pending_to_processing/
The hook doesn’t directly have the $order object but it can be found. This could be within your custom hooked function:
global $post; $order = new WC_Order( $post->ID ); $order_id = $order->get_order_number();
-
This reply was modified 6 years, 8 months ago by
Thomas Shellberg.
@shellbeezy I tried this hook and it does not work
function set_data_estimada_payment(){ global $post; $order_id = $post->ID; if ($order_id == 5926) { $dataPrazo = '2010-10-08'; add_post_meta( (int) $order_id, 'data_estimada', $dataPrazo, true ) ; update_post_meta($order_id, 'data_estimada',$dataPrazo); if ( ! add_post_meta( (int) $order_id, 'data_estimada', $dataPrazo, true ) ) { update_field('field_586d5f53a3be3', (string) $dataPrazo, $order_id); update_post_meta( (int) $order_id, 'data_estimada', $dataPrazo ); update_field('data_estimada', $dataPrazo, (int) $order_id); } } } add_action( 'woocommerce_order_status_pending_to_processing', 'set_data_estimada_payment', 10, 1 );
See I’m trying to update the ACF field in every way
In the call to add_action you have stated that you would pass one argument to your callback function yet you have have not passed any arguments to the callback function. PHP 7 will error on that so perhaps that’s the problem. You might want to check your error logs.
ok @wbrubaker I removed this argument but it still does not work.
I had already checked if these functions were being triggered. The problem is really the field that is not being updated
Would it be possible for the Order object to be overwriting the value of the target fields?Just remembering that this problem only occurs on the edit oerder screen in the admin panel. When the status change occurs on the order list screen, using the button in the actions column, everything works perfectly.
@felipe152 Just to recap, this is the translated version of what you asked initially:
I am using ACF to have some extra fields in my order that are automatically populated via programming when the status changes to “processing”. For this I am using woocommerce_order_status_changed. This is normal unless you change the status manually in the admmin page.
Can anyone tell me the pro that this occurs?This is the latest code you sent:
function set_data_estimada_payment(){ global $post; $order_id = $post->ID; if ($order_id == 5926) { $dataPrazo = '2010-10-08'; add_post_meta( (int) $order_id, 'data_estimada', $dataPrazo, true ) ; update_post_meta($order_id, 'data_estimada',$dataPrazo); if ( ! add_post_meta( (int) $order_id, 'data_estimada', $dataPrazo, true ) ) { update_field('field_586d5f53a3be3', (string) $dataPrazo, $order_id); update_post_meta( (int) $order_id, 'data_estimada', $dataPrazo ); update_field('data_estimada', $dataPrazo, (int) $order_id); } } } add_action( 'woocommerce_order_status_pending_to_processing', 'set_data_estimada_payment', 10, 1 );
I copied this code, made some updates and tested, and it is working correctly for me. The modified code, with notes, can be found here:
https://gist.github.com/jessepearson/9de93521ca60de56e6b4e44139c10d01If this is not working when you are modifying an order directly, then the issue may be a conflict with a plugin or your theme. It’s suggested you do the conflict troubleshooting mentioned here:
https://docs.woocommerce.com/document/woocommerce-self-service-guide/#section-4Thanks for your time @ jessepearson
But it still does not work
I did a new test.
I put an exit() before closing the set_data_estimada_payment function and looked at the database and the date was there. But when I removed this exit () and refreshed the page for the post to be resent, the date disappeared.
I’m assuming that the blank field of the edit order page is somehow influencing this.@felipe152 If the value is being set, then is being removed when you refresh the page, this means something is removing the meta data from the database. There must be a plugin or some code that is doing this.
Hello @jessepearson
I did the test removing the plugins but it did not work.
I’ve seen that the dump of the order object has the target but blank fields. I believe this is overwriting my data …Hi there,
This thread has been idle for a while. It seems like no one is able to continue this with you so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.
I can also recommend the following places for more development-oriented questions:
- WooCommerce Slack Community:?https://woocommerce.com/community-slack/
- Advanced WooCommerce group on Facebook:?https://www.facebook.com/groups/advanced.woocommerce/
Kind regards,
-
This reply was modified 6 years, 8 months ago by
- The topic ‘Alterar campo meta na página de pedido no admin’ is closed to new replies.