Search code examples
wordpresswoocommercetaxonomycustom-taxonomytaxonomy-terms

Get product custom taxonomy terms by product ID


I have registered two taxonomies for 'product' post type. called them pa_brand & pa_model

I have created a frontend form to edit the products which get the product data by $product_id from the URL Parameter.

What i need is to get the associated terms of pa_brand & pa_model by $product_id.

Following works only on the single product page, so that i can get the correct terms associated with the product.

$models = get_the_terms( $product->get_ID(), 'pa_model' );
echo '<pre>'; print_r( $models ); echo '</pre>';

$brands = get_the_terms( $product->get_ID(), 'pa_brand' );
echo '<pre>'; print_r( $brands ); echo '</pre>';

$cats = get_the_terms( $product->get_ID(), 'product_cat' );
echo '<pre>'; print_r( $cats ); echo '</pre>';

___________________________

RESULTS:

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 28
            [name] => FKS
            [slug] => fks
            [term_group] => 0
            [term_taxonomy_id] => 28
            [taxonomy] => pa_model
            [description] => 
            [parent] => 0
            [count] => 3
            [filter] => raw
        )

)

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 36
            [name] => MAN
            [slug] => man
            [term_group] => 0
            [term_taxonomy_id] => 36
            [taxonomy] => pa_brand
            [description] => 
            [parent] => 0
            [count] => 3
            [filter] => raw
        )

)

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 39
            [name] => Truck
            [slug] => truck
            [term_group] => 0
            [term_taxonomy_id] => 39
            [taxonomy] => product_cat
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )

)

But if the page is not the single product, which is http://example.com/?manage_product=206 , i get following WP_Error for both pa_brand & pa_model.

$models = get_the_terms( $product_id, 'pa_model' );
echo '<pre>'; print_r( $models ); echo '</pre>';

$brands = get_the_terms( $product_id, 'pa_brand' );
echo '<pre>'; print_r( $brands ); echo '</pre>';

$cats = get_the_terms( $product_id, 'product_cat' );
echo '<pre>'; print_r( $cats ); echo '</pre>';

___________________________

RESULTS:

WP_Error Object
(
    [errors] => Array
        (
            [invalid_taxonomy] => Array
                (
                    [0] => Invalid taxonomy.
                )

        )

    [error_data] => Array
        (
        )

    [additional_data:protected] => Array
        (
        )

)

WP_Error Object
(
    [errors] => Array
        (
            [invalid_taxonomy] => Array
                (
                    [0] => Invalid taxonomy.
                )

        )

    [error_data] => Array
        (
        )

    [additional_data:protected] => Array
        (
        )

)

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 39
            [name] => Truck
            [slug] => truck
            [term_group] => 0
            [term_taxonomy_id] => 39
            [taxonomy] => product_cat
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )

)

SHORTCODE

function frontend_add_product_form($product_id){

    global $product_id;
    $product = new WC_Product( $product_id );

    $koostis = get_the_terms( $product_id, 'pa_brand', array( 'fields' => 'names' ) );
    echo 'koostis<br><pre>'; print_r( $koostis ); echo '</pre>';
}
add_shortcode('frontend-add-product-form', 'frontend_add_product_form');

I've been stuck in it for hours and can't figure out, how do i get the associated terms from a custom taxonomy by product ID?


Solution

  • I found the issue. I had to add 0 to the init action for the custom custom Taxonomy.

    add_action( 'init', 'register_product_taxonomy', 0 );
    
    function register_product_taxonomy() {
    
        register_taxonomy(
            'pa_brand',
            'product',
            array(
                'label' => __( 'Brand' ),
                'rewrite' => array( 'slug' => 'car_brand' ),
                'hierarchical' => true,
            )
        );  
    
        register_taxonomy(
            'pa_model',
            'product',
            array(
                'label' => __( 'Model' ),
                'rewrite' => array( 'slug' => 'car_model' ),
                'hierarchical' => true,
            )
        );
    }