skip to Main Content

Is it possible to execute this php function when product is updated via Cron Job or REST_API?
Cron JOB is updating _stock2 meta field, and rest_api is updating basic stock quantity of woocommerce product.

add_action('woocommerce_admin_process_product_object', 'update_product_tag_on_stock_change');
function update_product_tag_on_stock_change($product) {
    // Debugging: Log message to check if the function is being called
    error_log('Function update_product_tag_on_stock_change called.');

    if ($product->is_type('variable')) {
        $variations = $product->get_children();

        $total_stock_quantity = 0;
        $total_stock_quantity2 = 0;

        foreach ($variations as $variation_id) {
            $variation = wc_get_product($variation_id);
            $total_stock_quantity += intval($variation->get_stock_quantity()) - intval($variation->get_meta('_stock2'));
            $total_stock_quantity2 += intval($variation->get_meta('_stock2'));
        }

        // Debugging: Log total_stock_quantity and total_stock_quantity2
        error_log('Total Stock Quantity: ' . $total_stock_quantity);
        error_log('Total Stock Quantity2: ' . $total_stock_quantity2);

        if ($total_stock_quantity2 > 0 && $total_stock_quantity < 2) {
            wp_set_object_terms($product->get_id(), '3-5dni', 'product_tag', true);
            // Debugging: Log message when tag is set
            error_log('Tag set to 3-5dni');
        } else {
            wp_remove_object_terms($product->get_id(), '3-5dni', 'product_tag', true);
            // Debugging: Log message when tag is removed
            error_log('Tag removed from 3-5dni');
        }
    } else {
        $stock_quantity = intval($product->get_stock_quantity()) - intval($product->get_meta('_stock2'));
        $stock_quantity2 = intval($product->get_meta('_stock2'));

        // Debugging: Log stock_quantity and stock_quantity2
        error_log('Stock Quantity: ' . $stock_quantity);
        error_log('Stock Quantity2: ' . $stock_quantity2);

        if ($stock_quantity2 > 0 && $stock_quantity < 2) {
            wp_set_object_terms($product->get_id(), '3-5dni', 'product_tag', true);
            // Debugging: Log message when tag is set
            error_log('Tag set to 3-5dni');
        } else {
            wp_remove_object_terms($product->get_id(), '3-5dni', 'product_tag', true);
            // Debugging: Log message when tag is removed
            error_log('Tag removed from 3-5dni');
        }
    }
}

Now this function works only if I update product manualy via admin page.

2

Answers


  1. Your action specifically targets the admin processing of the object, so it won’t execute unless this hook is being executed: woocommerce_admin_process_product_object

    However, there are specific hooks you could target in addition to the one you have above and you could have your function added to multiple hooks:

    add_action( 'woocommerce_admin_process_product_object', 'update_product_tag_on_stock_change' );
    add_action( 'woocommerce_update_product', 'update_product_tag_on_stock_change', 10, 1 );
    
    Login or Signup to reply.
  2. If you want to trigger your function when product is updated via the admin, via a Cron Job or via REST_API request, you should better use woocommerce_update_product hook located in WC_Product_Data_Store_CPT Class, that has 2 arguments: $product_id and $product.

    Important notes:

    • If using a Cron Job, WC_Product CRUD objects setter methods need to be used instead of WordPress post functions or direct SQL UPDATE queries, to trigger WooCommerce related hooks.
    • The hook woocommerce_update_product is only triggered when updating an existing product.

    Now your current code can be optimized and compacted, removing repetitive things. Also, you missed checking if the product tag "3-5dni" is set or not in the product, before trying to add it or remove it.

    Try the following revised code:

    add_action( 'woocommerce_update_product', 'update_product_tag_on_stock_change', 10, 2 );
    function update_product_tag_on_stock_change( $product_id, $product ) {
        $taxonomy  = 'product_tag';
        $term_slug = '3-5dni';
        error_log('Function "update_product_tag_on_stock_change" executed.'); // Debugging (function executed)
        error_log('Product type: '.$product->get_type() ); // Debugging (product type)
    
        if ( $product->is_type('variable') ) {
            $stock_quantity = $stock_quantity2 = 0; // Initializing variables
    
            // Loop through available variations (array of variations objects)
            foreach ( $product->get_available_variations('objects') as $variation ) {
                $stock_quantity  += intval($variation->get_stock_quantity()) - intval($variation->get_meta('_stock2'));
                $stock_quantity2 += intval($variation->get_meta('_stock2'));
            }
        } else {
            $stock_quantity  = intval($product->get_stock_quantity()) - intval($product->get_meta('_stock2'));
            $stock_quantity2 = intval($product->get_meta('_stock2'));
        }
        error_log('Stock Quantity: ' . $stock_quantity); // Debugging: stock_quantity
        error_log('Stock Quantity2: ' . $stock_quantity2); // Debugging: stock_quantity2
    
        if ( $stock_quantity2 > 0 && $stock_quantity < 2 
        && ! has_term($term_slug, $taxonomy, $product_id) ) {
            wp_set_post_terms($product_id, $term_slug, $taxonomy, true); 
            error_log('Added tag "3-5dni".'); // Debugging: Tag added
        } elseif ( has_term($term_slug, $taxonomy, $product_id) ) {
            wp_remove_object_terms($product_id, $term_slug, $taxonomy);
            error_log('Removed tag "3-5dni".'); // Debugging: Tag removed
        }
    }
    

    It should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search