ACF & Woocommerce: Product variation is not named properly

My generated product variation is not named properly which therefor is not valid.

how it looks: Screen

I’m working on a WooCommerce project where I need to automatically create product variations based on ACF (Advanced Custom Fields) repeater fields. The repeater fields contain two subfields: aantal_kms (distance) and prijs (price). I want each variation to be named with the distance value and to have the correct attributes set.

Here is what I have done so far:

I have set up the ACF repeater fields on the product edit page.
I wrote a function to create the product variations on save, using the data from the ACF repeater fields.

The variations are being created, but the titles of the variations are not set correctly. Each variation should have a title that includes the distance value and should have the corresponding attribute set. This makes the varitions invalid so it doesnt show on the front end.variation name

Here is the code I’m currently using:

function add_variations_from_acf_fields($post_id) {
    // Check if this is a product and we're not in an autosave
    if (get_post_type($post_id) !== 'product' || (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) {
        return;
    }

    // Get the repeater field data
    $acf_repeater = get_field('Deelnameprijzen', $post_id);

    // Check if there is any data in the repeater field
    if ($acf_repeater) {
        // Load the product
        $product = wc_get_product($post_id);
        
        // Delete existing variations
        if ($product->is_type('variable')) {
            $existing_variations = $product->get_children();
            foreach ($existing_variations as $variation_id) {
                wp_delete_post($variation_id, true);
            }
        } else {
            // If the product is not already a variable product, change its type
            wp_set_object_terms($post_id, 'variable', 'product_type');
            $product = wc_get_product($post_id);
        }

        // Prepare attributes
        $distances = array();
        foreach ($acf_repeater as $repeater_item) {
            $distances[] = $repeater_item['aantal_kms'];
        }

        // Ensure distances are unique and filter out any unexpected data
        $distances = array_unique(array_filter($distances));

        // Set the 'Aantal Kms' attribute
        $product_attributes = array();
        $product_attributes['pa_aantal_kms'] = array(
            'name'         => 'Aantal Kms',
            'value'        => implode(' | ', $distances),
            'position'     => 0,
            'is_visible'   => 1,
            'is_variation' => 1,
            'is_taxonomy'  => 0
        );
        update_post_meta($post_id, '_product_attributes', $product_attributes);

        // Create new variations based on the repeater field data
        foreach ($acf_repeater as $repeater_item) {
            $variation = new WC_Product_Variation();
            $variation->set_parent_id($post_id);

            // Set variation attributes
            $variation_attributes = array(
                'pa_aantal_kms' => $repeater_item['aantal_kms']
            );
            $variation->set_attributes($variation_attributes);

            // Set variation price
            $variation->set_regular_price($repeater_item['prijs']);

            // Save the variation
            $variation->save();
        }
    }
}
add_action('save_post', 'add_variations_from_acf_fields', 10, 1);

I ensured the variation title includes the distance value.
The terms are created if they don’t exist.
The attribute values are set for each variation.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật