Home › Forums › Backend Issues (wp-admin) › ACF on product variations: almost works › Reply To: ACF on product variations: almost works
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
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.