按产品 ID 获取产品自定义分类术语

Get product custom taxonomy terms by product ID

我已经为 'product' post 类型注册了两个分类法。叫他们 pa_brand & pa_model

我创建了一个前端表单来编辑通过 URL 参数中的 $product_id 获取产品数据的产品。

我需要的是通过 $product_id.

获取 pa_brand & pa_model 的相关条款

以下作品仅在单个产品页面上,以便我可以获得与产品相关的正确条款。

$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
        )

)

但是如果该页面不是单一产品,即http://example.com/?manage_product=206,我会关注WP_Error 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
        )

)

短代码

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');

我已经陷入其中数小时,无法弄清楚,我如何通过产品 ID 从自定义分类法中获取相关术语?

我发现了问题。我必须将 0 添加到自定义分类法的初始化操作中。

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,
        )
    );
}