0

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?

Nash
  • 285
  • 2
  • 15
  • Refer to this answer here [Get custom product attributes in Woocommerce](https://stackoverflow.com/questions/13374883/get-custom-product-attributes-in-woocommerce) – Bhautik Mar 19 '21 at 06:50
  • @Bhautik Nothing from them is working.. tried the multiple options – Nash Mar 19 '21 at 07:07
  • Where did you put this code? – Bhautik Mar 19 '21 at 07:07
  • @Bhautik in the shortcode used for product forms. I have added this to my post above – Nash Mar 19 '21 at 07:17
  • `$koostis = get_the_terms( $product_id, 'pa_brand', array( 'fields' => 'names' ) );` doesn't 't have third parameter. – Bhautik Mar 19 '21 at 07:19
  • @Bhautik I have tried all those `$koostis = array_shift( wc_get_product_terms( $product_id, 'pa_brand' ) );` / `$koostis = array_shift( wc_get_product_terms( $product_id, 'pa_brand', array( 'fields' => 'names' ) ) )` / `$koostis = $product->get_attribute( 'pa_brand' );` . mostly they show empty array or empty value – Nash Mar 19 '21 at 07:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230115/discussion-between-bhautik-and-nash). – Bhautik Mar 19 '21 at 07:28

1 Answers1

0

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,
        )
    );
}
Nash
  • 285
  • 2
  • 15