• I want to show woocommerce category list in the plugin fields. But its not working for me. Here is my code so far

    <?php
    use Carbon_Fields\Container;
    use Carbon_Fields\Field;

    Class asd_plugin_Settings {
    function __construct() {
    add_action( ‘init’, array($this, ‘get_cats’) );
    add_action( ‘carbon_fields_register_fields’, array($this,’crb_attach_theme_options’) );
    add_action( ‘after_setup_theme’, array($this,’make_crb_load’) );
    }

    public function crb_attach_theme_options() {
    Container::make( ‘theme_options’, __( ‘Theme Options’, ‘crb’ ) )
    ->add_fields( array(
    Field::make( “multiselect”, “crb_available_cats”, “Category” )
    ->add_options( $this->get_product_cats() ),
    ) );
    }

    public function make_crb_load() {
    require_once( ASD_PLUGIN_PATH . ‘/carbon-fields/vendor/autoload.php’ );
    \Carbon_Fields\Carbon_Fields::boot();
    }

    public function get_cats() {
    $categories = get_terms( ‘product_cat’, ‘orderby=name&hide_empty=0’ );
    $cats = array();
    if ( $categories )
    foreach ( $categories as $cat )
    $cats[$cat->term_id] = esc_html( $cat->name );

    print_r($cats); //getting category properly
    }

    public function get_product_cats() {
    $categories = get_terms( ‘product_cat’, ‘orderby=name&hide_empty=0’ );
    $cats = array();

    if ( $categories )
    foreach ( $categories as $cat )
    $cats[$cat->term_id] = esc_html( $cat->name );

    return $cats; //not getting category. Showing error invalid taxonomy
    }

    }

  • The topic ‘Get woocommerce category list in fields’ is closed to new replies.