Which hook to save WooCommerce product meta in WooCommerce?
Asked Answered
F

2

6

I am trying to update a custom meta on Woocommerce product update event. I've read that I should use woocommerce_update_product rather than save_post but I cannot figure out why only save_post works in my case.

Below code works

add_action( 'save_post', 'mp_sync_on_product_save', 20);
function mp_sync_on_product_save( $product_id ) {
     update_post_meta($product_id, 'test_acf_product', "text");
};

Below code does not work

add_action( 'woocommerce_update_product', 'mp_sync_on_product_save', 20);
function mp_sync_on_product_save( $product_id ) {
    update_post_meta($product_id, 'test_acf_product', "text");
};

I accidentally found that if I add exit; at the end as below, the above code works (breaks page but meta saved in DB)

add_action( 'woocommerce_update_product', 'mp_sync_on_product_save', 20);
function mp_sync_on_product_save( $product_id ) {
    update_post_meta($product_id, 'test_acf_product', "text");
    exit;
};

I can get away with save_post but I'd love to know why woocommerce_update_product won't work I'd appreciate if anyone could give me some hint.

Thank you!

Fellows answered 14/6, 2020 at 17:30 Comment(0)
Z
10

There are multiple ways to save product meta data when saving the product in backend:

1) Since WooCommerce 3 you can use:

add_action( 'woocommerce_admin_process_product_object', 'action_save_product_meta' );
function action_save_product_meta( $product ) {
    $product->update_meta_data( 'test_acf_product', 'text' );
}

2) Or the old WooCommerce way:

add_action( 'woocommerce_process_product_meta', 'action_save_product_meta' );
function action_save_product_meta( $product_id ) {
    update_post_meta($product_id, 'test_acf_product', 'text' );
}

3) Or the Wordpress way (targeting "product" custom post type):

add_action( 'save_post_product', 'action_save_product_data', 20);
function action_save_product_data( $post_id ) {
     update_post_meta($post_id, 'test_acf_product', 'text');
}

4) Or also using save_post hook (and targeting "product" custom post type, to avoid this meta data to be saved for all posts types and custom post types):

add_action( 'save_post', 'action_save_product_data', 20);
function action_save_product_data( $post_id ) {
    global $typenow;

    if ( 'product' === $typenow ) {
        update_post_meta($post_id, 'test_acf_product', 'text');
    }
}
Zarah answered 14/6, 2020 at 22:42 Comment(5)
Thanks for your reply! However, only (4) works in my case which is very weird. I use WooCommerce 4.0.1, Wordpress 5.4.2, PHP 7.4.7. Is there a way that I can debug why other methods are not working for me? I turned on WP_DEBUG but there's no error with the codes. When I click Update Product, the edit page refreshes normally but the meta data is not saved.Fellows
@Fellows You should try ti clarify your question as we don't really know what you are trying to do and where, on what product type… The first 2 hooks are the right WooCommerce hooks to Add/update product meta on admin product pages (for WooCommerce "product" post type). For product variations, it's a different hook for example as it's "product_variation" post type.Zarah
You can see all this working related answer for woocommerce_admin_process_product_object hook and also for woocommerce_process_product_meta hookZarah
Thank you for your help, I will research further why other hooks are not working for me!Fellows
I had the same issue - only(4) works. If I use any of the other hooks - it is updated, then overwritten back to the original value. I am not sure why but I did not investigate either.Frisette
S
0

The answer from @loictheaztec is almost right. To save the data with CRUD you need to run the save_meta_data() command:

add_action( 'woocommerce_admin_process_product_object', 'action_save_product_meta' );
function action_save_product_meta( $product ) {
    $product->update_meta_data( 'test_acf_product', 'text' );
    $product->save_meta_data();
}
Supercool answered 9/1, 2023 at 12:49 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.