4
votes

I've created a variable product using woocommerce plugin. In the variable product I've created a custom attribute "License Duration" with values "3 Months | 6 Months | 1 Year | Life Time".

Now I want to display this attribute on a custom single product template.

I've tried following solutions

1.

$licenseDurations = get_the_terms( $post->ID, 'attribute_license-duration' );

This shows following on var_dump($licenseDurations); `

object(WP_Error)[558]
  public 'errors' => 
    array (size=1)
      'invalid_taxonomy' => 
        array (size=1)
          0 => string 'Invalid taxonomy' (length=16)
  public 'error_data' => 
    array (size=0)
    empty`

and

2.

global $product;
$licenseDuration = $product->get_attribute( 'License Duration' ); // Here I also tried attribute slug `attribute_license-duration` instead of `License Duration` but no use

Ref: https://stackoverflow.com/a/13454788/2758870

and

3.

global $product;
$licenseDurations = get_the_terms( $product->id, 'License Duration');

foreach ( $licenseDurations as $licenseDuration ) 
{
    echo $licenseDuration->name;
}

in both of above cases I got nothing because $product; is returning null on var_dump($product)

4. I've also tried this code http://isabelcastillo.com/woocommerce-product-attributes-functions

by directly calling isa_woo_get_one_pa() where I want to show values. but with no luck.

Anyone here please help...

3
Product attributes start with pa_ . In your first attempt, change attribute_license-duration to pa_license-durationAnand Shah
@AnandShah... I tried pa_license-duration but it is returning Invalid taxonomy error on var_dump. any other solution pleaseCode Poet
I have checked it locally by creating an attribute with the same name and it works fine.Anand Shah
@AnandShah... I dumped the database and did not find pa_license-duration attribute on search. Instead I found attribute_license-durationCode Poet
That sounds strange, which version of WooCommerce are you using? Navigate to Products -> Attributes, click on "License Duration" and check the URL, what does the URL look like?Anand Shah

3 Answers

4
votes

You can try the following

1.get_post_meta to get the product attributes

$attr = get_post_meta( 123, '_product_attributes' ); // replace 123 with the actual product id 
print_r( $attr );

2.Or you can create a product object and then using the get_attributes method

$p = new WC_Product( 123 ); // // replace 123 with the actual product id 
print_r( $p->get_attributes() );
1
votes

Not sure if there is changes in woocommerce, above did not give the attribute value. Following post from this page gave the correct answer

// replace pa_attribute with your attribute name, but keep the pa_ prefix
// e.g. pa_weight

$fabric_values = get_the_terms( $product->id, 'pa_attribute');

foreach ( $fabric_values as $fabric_value ) {
  echo $fabric_value->name;
}
0
votes

Here are all product attributes listing. Hope this helps you.

$product_attributes = get_post_meta( $post->ID, '_product_attributes' ); 
foreach ( $product_attributes as $group_attributes ) {
    foreach ( $group_attributes as $attributes ) {
      echo $attributes['name'].'=>'.$attributes['value'];
    }
}