4

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...

Community
  • 1
  • 1
Code Poet
  • 147
  • 1
  • 3
  • 16
  • Product attributes start with `pa_` . In your first attempt, change `attribute_license-duration` to `pa_license-duration` – Anand Shah Sep 28 '15 at 15:37
  • @AnandShah... I tried `pa_license-duration` but it is returning **Invalid taxonomy** error on `var_dump`. any other solution please – Code Poet Sep 29 '15 at 05:52
  • I have checked it locally by creating an attribute with the same name and it works fine. – Anand Shah Sep 29 '15 at 05:54
  • @AnandShah... I dumped the database and did not find `pa_license-duration` attribute on search. Instead I found `attribute_license-duration` – Code Poet Sep 29 '15 at 07:38
  • 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 Sep 29 '15 at 08:01
  • @AnandShah... I am not using attributes tab I am talking about creating variable products on **Add Product** page using **Product data** area and creating custom attributes in **attributes** tab of **product data** area and then using these attributes as variations in **variations** tab of **product data** area. – Code Poet Sep 29 '15 at 10:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90875/discussion-between-angrycoder-and-anand-shah). – Code Poet Sep 29 '15 at 10:50

3 Answers3

4

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() );
Anand Shah
  • 14,575
  • 16
  • 72
  • 110
1

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;
}
ken
  • 13,869
  • 6
  • 42
  • 36
0

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'];
    }
}
vadivel a
  • 1,754
  • 1
  • 8
  • 18