Home Forums Backend Issues (wp-admin) ACF on product variations: almost works Reply To: ACF on product variations: almost works

  • @penskyc @alicinaroglu

    I ran into the same problem. I fixed it bay changing the woocommerce_save_product_variation function to:

    add_action('woocommerce_save_product_variation', function ($variation_id, $i = -1) {
      if (!empty($_POST['acf']) && is_array($_POST['acf']) && array_key_exists($i, $_POST['acf']) && is_array(($fields = $_POST['acf'][$i]))) {
        $unique_updates = array();
        foreach ($fields as $key => $val) {
          if (is_array($val)) {
            // repeater fields need to be parsed separately
            foreach ($val as $repeater_key => $repeater_val) {
              if (!array_key_exists($repeater_key, $unique_updates) || !empty($repeater_val)) {
                $unique_updates[$repeater_key] = $repeater_val;
          } else {
            // non-repeater fields can be parsed normally
            // The repeater fields are repeated here, but empty. This causes the repeater that was updated above to be cleared
            if (!array_key_exists($key, $unique_updates) || !empty($val)) {
              $unique_updates[$key] = $val;
        // Only update each field once
        foreach ($unique_updates as $key => $val) {
          update_field($key, $val, $variation_id);
    }, 10, 2);

    The problem was that the repeater fields were in the fields array twice. The first time would set it, but the second would clear the repeater again. So I just made sure that each field was only updated once