kh1
kh1

Reputation: 120

How to get the post term name of a custom taxonomy in Woocommerce

In Woocommerce, I am trying to change (add some) in plugin file Retailcrm для wordpress/woocommerce, Github Especially the part that will allow me to add the name of the custom taxonomy of the product (related to the manufacturer) to xml.

The problem is that something is wrong with my code, I can not get the name of the taxonomy.

This is the original part of php file:

$product_data = array(
            'id' => $product->get_id(), 
            'productId' => ($this->get_parent_product($product) > 0) ? $parent->get_id() : $product->get_id(),
            'name' => $product->get_name(),
            'productName' => ($this->get_parent_product($product) > 0) ? $parent->get_title() : $product->get_title(),
            'price' => $this->get_price_with_tax($product),
            'purchasePrice'=> WC_COG_Product::get_cost($product),
            'picture' => $image[0],
            'url' => ($this->get_parent_product($product) > 0) ? $parent->get_permalink() : $product->get_permalink(),
            'quantity' => is_null($product->get_stock_quantity()) ? 0 : $product->get_stock_quantity(),
            'categoryId' => $term_list,
            'dimension' => $dimension,
            'weight' => $weight,
            'tax' => isset($tax) ? $tax['rate'] : 'none'
        );

I have a taxonomy pa_proizvoditel, this is an attribute of the product.

My guess is to do this:

$vendor = get_term( $product, 'pa_proizvoditel' );
        $product_data = array(
            'id' => $product->get_id(), 
            'productId' => ($this->get_parent_product($product) > 0) ? $parent->get_id() : $product->get_id(),
            'name' => $product->get_name(),
            'productName' => ($this->get_parent_product($product) > 0) ? $parent->get_title() : $product->get_title(),
            'price' => $this->get_price_with_tax($product),
            'purchasePrice'=> WC_COG_Product::get_cost($product),
            'vendor'=> $vendor->name,
            'picture' => $image[0],
            'url' => ($this->get_parent_product($product) > 0) ? $parent->get_permalink() : $product->get_permalink(),
            'quantity' => is_null($product->get_stock_quantity()) ? 0 : $product->get_stock_quantity(),
            'categoryId' => $term_list,
            'dimension' => $dimension,
            'weight' => $weight,
            'tax' => isset($tax) ? $tax['rate'] : 'none'
        );

That is to add:

$vendor = get_term( $product, 'pa_proizvoditel' );

and

'vendor'=> $vendor->name,

As the plugin developer suggested, but in this case in xml we will see the product name in <vendor>.

How to get the name of this taxonomy correctly?

Upvotes: 1

Views: 614

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

Updated:

As it is product attribute (starting with "pa_"), you can simply use in your code array the WC_Product method get_attribute(), that will give you the term name:

'vendor'=> $product->get_attribute( 'pa_proizvoditel' );

Upvotes: 2

Related Questions