ACF & Woocommerce: Automatically Set Product Variations and Attributes Based on ACF Repeater Fields

  Kiến thức lập trình

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.

how it looks: Screen

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.

New contributor

Anton Pycarelle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT