Hi @yonish3
It seems that the woocommerce_update_product hook fires twice. You can read more on it here: https://stackoverflow.com/questions/59527908/woocommerce-update-product-action-fire-only-once-for-every-product-update . Can you please see if the workaround discussed in the thread work for you? You may need to tweak it a bit.
Here’s more information about the hook: http://hookr.io/actions/woocommerce_update_product/
Thanks, Margaret.
I tried adding the function code inside like this:
add_action( 'woocommerce_update_product', 'update_on_product_save', 1000, 1 );
function update_on_product_save($product_id, $product) {
$updating_product_id = 'update_product_' . $product_id;
if ( false === ( $updating_product = get_transient( $updating_product_id ) ) ) {
// do something
set_transient( $updating_product_id , $product_id, 2 );
}
}
But it still doesn’t work.
I also don’t this it’s the issue.
According to the link you shared, the issue is that the function will run twice.
But the issue I have is that the function runs only after the second time I click the update button. i.e targeting the woocommerce_update_product hook.
It seems that the issue was with ACF update the DB after the hook is triggered.
Therefore the value updated was always the value from the prevuis update.
Have slved it using this function:
add_action('acf/save_post', 'woocommerce_update_product_acf_save_post', 1000, 1);
function woocommerce_update_product_acf_save_post( $product_id ) {
if (get_post_type($product_id) == "product") {
remove_action('acf/save_post', 'woocommerce_update_product_acf_save_post');
update_on_product_save( $product_id );
}
}
function update_on_product_save( $product_id ) {
do somthing
}
Hi @yonish3!
Awesome! We’re happy to hear that you figured it out! I will mark this as resolved but feel free to reach out to us if you have any other questions!
Also, thank you for sharing the solution with the community!
My very best,
Hi
I’m having the same issue, I send an email to users who are subscribed to a products. But it e-mails the previous changes. My current code reads;
add_action('woocommerce_update_product', 'action_on_product_save', 10, 2);
function action_on_product_save($product_id, $product) {
$updating_product_id = 'update_product_' . $product_id;
if (false === ($updating_product = get_transient($updating_product_id))) {
Should I replace my
add_action('woocommerce_update_product', 'action_on_product_save', 10, 2);
with
add_action(‘acf/save_post’, ‘woocommerce_update_product_acf_save_post’, 1000, 1);
function woocommerce_update_product_acf_save_post( $product_id ) {
if (get_post_type($product_id) == “product”) {
remove_action(‘acf/save_post’, ‘woocommerce_update_product_acf_save_post’);
update_on_product_save( $product_id );
}
}`
or am I reading this completly wrong?
-
This reply was modified 2 years, 4 months ago by
jordym. Reason: markup
Yes, this is what solved it for me.
I’m not a PHP WP developer, so it was a lot to search until I found something that worked for me.
I hope it will help you as well, but I can say that for sure.
I have been testing a bit; but i’m running into a weird issue;
This code works, but then I have to drop some code as I have two arguments, besides Product ID, I also need Product, as I need the productname and product image id.
add_action('acf/save_post', 'woocommerce_update_product_acf_save_post', 1000, 1);
function woocommerce_update_product_acf_save_post( $product_id ) {
if (get_post_type($product_id) == "product") {
remove_action('acf/save_post', 'woocommerce_update_product_acf_save_post');
update_on_product_save( $product_id );
}
}
##add_action('woocommerce_update_product', 'action_on_product_save', 20, 2);
function update_on_product_save($product_id) {
So when I try it like,
add_action('acf/save_post', 'woocommerce_update_product_acf_save_post', 1000, 2);
function woocommerce_update_product_acf_save_post( $product_id, $product ) {
if (get_post_type($product_id) == "product") {
remove_action('acf/save_post', 'woocommerce_update_product_acf_save_post');
update_on_product_save( $product_id, $product );
}
}
##add_action('woocommerce_update_product', 'action_on_product_save', 20, 2);
function update_on_product_save($product_id, $product ) {
I get an error;
Uncaught ArgumentCountError: Too few arguments to function woocommerce_update_product_acf_save_post(), 1 passed in /home/domain/public_html/wp-includes/class-wp-hook.php on line 307 and exactly 2 expected in /home/domain/public_html/wp-content/themes/hello-elementor-child/functions.php:50, referer: https://www.domain.com/wp-admin/post.php?post=7398&action=edit
Any tips in resolving this?
Don’t add arguments to the function declaration.
Use $product_id, which is part of the way WP works, to find the value you need.
Maybe something like this:
$varible = get_field( 'field_name', $product_id );
Or
$product_id = get_the_ID();
$varible = get_post_meta( $product_id, 'field_name', true );
Or
global $product;
$varible = get_field( 'field_name', $product->get_id() );
Or
global $product;
$varible_2 = $product->get_attribute( 'attribute_name' );
It depends on wich file you add the code to.
I hope one of these will solve your issue.
Hi,
I got it working with the following code =) Thanks for putting me on the correct path!
function update_on_product_save($product_id) {
global $product;
$product = wc_get_product( $product_id );
$product_title = $product->get_name();
$product_image_id = $product->get_image_id();