Support

Account

Home Forums General Issues Dynamic Select for Tax Classes

Solved

Dynamic Select for Tax Classes

  • First let me join the chorus of people praising this product!!! Between the quality of the product and documentation to the robust community and support, congratulations and thanks!

    Okay, so, I’m a rusty PHP developer, but I’ve been writing a plugin that captures a bunch of data from ACF associated with a custom post type and then uses that data to generate a Woocommerce product programmatically.

    In my ACF field group, I need to populate a select field with the available tax classes. I tried the code below, but it fails:

    /**
     * ACF Dynmaic Select of Tax Classes
     * 
     */
    function get_tax_classes() {
        return array_filter( array_map( 'trim', explode( "\n", get_option( 'woocommerce_tax_classes' ) ) ), array( __CLASS__, 'is_valid_tax_class' ) );
    }
    
    function tax_class_options($field) {
        $field['choices'] = array();
        // reset choices
        $options =  WC_Tax::get_tax_classes();
        
        
        // get the textarea value from options page without any formatting
        //$choices = get_field('my_select_values', 'option', false);
    
        
        // explode the value so that each line is a new array piece
        //$choices = explode("\n", $choices);
    
        
        // remove any unwanted white space
        //$choices = array_map('trim', $choices);
    
        
        // loop through array and add to field 'choices'
        if( is_array($options) ) {
            
            foreach( $options as $option ) {
                
                $field['choices'][ $option ] = $option;
                
            }
            
        }
        
    
        // return the field
        return $field;
        
    }
    
    add_filter('acf/load_field/key=field_5d88cd256f08a', 'tax_class_options');

    As you can see, I tried to use the example from ACF docs, and it doesn’t look right to me. The menu returns empty.

  • I solved this pretty quickly. My issue was with the function to get the tax classes. It was based on an old version of WP.

    The correct version looks like this:

    function get_tax_classes() {
        return WC_Tax::get_tax_classes();
    }

    The rest of the code worked great. Thanks again ACF!

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Dynamic Select for Tax Classes’ is closed to new replies.