0

I have an action in my functions.php to inform subscribers of an product update. I have a working function that accepts 2 arguments ($product_id and $product). However when I press update on a product an e-mail goes out with the old values of my ACFs.

add_action('woocommerce_update_product', 'action_on_product_save', 20, 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))) {

I found a topic from someone with the same issue, but this solution won’t accept 2 arguments for some reason (probably my fault).

this is the code I found online which works if I uncomment all the $product related content further down. So with the code below my e-mail shows the updated ACF straight away. However without the productname as I can't add the $product argument without getting the Fatal error (see below)

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) {

When I add the second argument however (see me change the arguments value to 2 below)

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 );
    }
}

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

3
  • 1
    That's correct, acf/save_post only "passes one argument", you can check it here advancedcustomfields.com/resources/acf-save_post Oct 31, 2022 at 8:37
  • How can I use the $product to retrieve the product name inside that action?
    – J. Minnebo
    Oct 31, 2022 at 11:32
  • I got it working with the following code! ``` 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(); ```
    – J. Minnebo
    Oct 31, 2022 at 18:00

1 Answer 1

0

I got it working with this! :)

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.