Support

Account

Home Forums ACF PRO Dynamically creating field based on select value Reply To: Dynamically creating field based on select value

  • This is the current code i have in functions.php;

    
    /* --
    ACF Option page
    -- */
    if (function_exists('acf_add_options_page')) {
    
        // Main
        acf_add_options_page(array(
            'page_title'     => 'Product tabs',
            'menu_title'    => 'Product tabs',
            'menu_slug'     => 'acf-product-tabs',
            'capability'    => 'edit_posts',
            'icon_url' => 'dashicons-table-row-after',
            'redirect'        => true,
            'position' => '20.0'
        ));
    }
    
    /* --
    WooCommerce Tabs
    -- */
    add_filter('woocommerce_product_tabs', 'woo_dynamic_tabs');
    function woo_dynamic_tabs($tabs)
    {
        if (have_rows('repeater_field_name', 'option')) :
    
            while (have_rows('repeater_field_name', 'option')) : the_row();
                $woo_tab_label = get_sub_field('repeater_sub_field');
    
                // Attributen tab
                $tabs['attrib_' . strtolower($woo_tab_label) . '_tab'] = array(
                    'title' => __($woo_tab_label, 'woocommerce'),
                    'priority' => 100,
                    'callback' => 'woo_tab_content'
    
                );
    
            // End loop.
            endwhile;
    
        // No value.
        else :
        // Do something...
        endif;
    
        // return
        return $tabs;
    }
    
    /* --
    Fill button group
    -- */
    add_filter('acf/load_field/key=field_xxxxxxxxxxx', function ($field) {
    
        // Add options to select field
        $field['choices'] = array();
    
        // Dynamic data
        $created_tabs = get_field('repeater_field_name', 'option');
    
        foreach ($created_tabs as $new_tab) {
            $field['choices'][$new_tab['repeater_sub_field']] = $new_tab['repeater_sub_field'];
        }
    
        return $field;
    });
    
    /* --
    WooCommerce Tabs content
    -- */
    function woo_tab_content($slug, $tab)
    {
        // Dynamic creation of wysiwig fields for single product
        // How?
    }