skip to Main Content

Can anyone tell me which action precedes the woocommerce_save_product_variation action hook?

I’m trying to save old price and quantity before updating product variation

3

Answers


  1. Chosen as BEST ANSWER

    I solved with woocommerce_variation_header.

    This hook will be activated for each product variation. Therefore, you need to have a private class-level associative array, where you will store (for you) important information from product (variation), e.g. price, stock quantity, etc.

    After that, save that array in db, ie. wp_options, with a specific name.

    When you call woocommerce_save_product_variation, then also call array that you already saved for same variation from wp_options and finally then compare the data.

    That’s what I needed but a detour and reason for this approach is that I had to stay on the same old version of WooCommerce.

    Thank you all for participating!


  2. In meta-boxes/class-wc-meta-box-product-data.php line 530-541 you can find

    /**
     * Set variation props before save.
     *
     * @param object $variation WC_Product_Variation object.
     * @param int $i
     * @since 3.8.0
     */
    do_action( 'woocommerce_admin_process_variation_object', $variation, $i );
    
    $variation->save();
    
    do_action( 'woocommerce_save_product_variation', $variation_id, $i );
    

    So woocommerce_admin_process_variation_object action precedes woocommerce_save_product_variation

    Login or Signup to reply.
  3. Use woocommerce_admin_process_variation_object

    add_action('woocommerce_admin_process_variation_object', 'prefix_previous_variation_price', 10, 1);
    function prefix_previous_variation_price($variation) {
       $previous_price = $variation->get_price();
    }
    

    To find this kind of hook you can look woocommerce source files.
    For example for this: I search the woocommerce_save_product_variation on http://hookr.io/. I saw that this hook was called in class-wc-meta-box-product-data.php. Then I take a look to this file and I find woocommerce_admin_process_variation_object hook who are called just before $variation->save();

    Take a look here:
    https://github.com/woocommerce/woocommerce/blob/ac9f83d7724b889a1740d651eb4cd8ac5bb5b4f2/includes/admin/meta-boxes/class-wc-meta-box-product-data.php#L537

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