2

I need to get product attributes from product in WooCommerce, type product Simple product, some product can be as dropdown, but much options as radio button.

How I can do this? How it possible?

With variations I have no problem, but for simple I can't get them.

0
1

Building on Loic's answer for an attribute of pa_manufacturer:

if ( get_post_type( $post ) === 'product' && ! is_a($product, 'WC_Product') ) {
    $product = wc_get_product( get_the_id() ); // Get the WC_Product Object
}

$product_attributes = $product->get_attributes(); // Get the product attributes

// Output
$manufacturer_id = $product_attributes['pa_manufacturer']['options']['0']; // returns the ID of the term
$manufacturer_name = get_term( $manufacturer_id )->name; // gets the term name of the term from the ID
echo '<p class="manufacturer">'.$manufacturer_name.'</p>'; // display the actual term name
5

You will use the WC_Product method get_attributes() that returns an array like:

global $product;

if ( get_post_type( $post ) === 'product' && ! is_a($product, 'WC_Product') ) {
    $product = wc_get_product( get_the_id() ); // Get the WC_Product Object
}

$product_attributes = $product->get_attributes(); // Get the product attributes

// Raw output
echo '<pre>'; print_r( $product_attributes ); echo '</pre>';

It should work (for real product attributes).


Now if you use some third party plugins, like product add-Ons for example, they add some custom fields to the product, but they are not product attributes…

2
  • it is not working.
    – abu abu
    Oct 24 '20 at 3:06
  • This gets an array of all attributes, which can be used to get an individual attribute by replacing print_r($product_attributes) with something like print_r($product_attributes['pa_color']) however that only returns an ID of the term, so more would need to be done to get the term title from that ID.
    – Nathan
    Oct 25 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.