Get custom product attributes in Woocommerce

后端 未结 9 757
失恋的感觉
失恋的感觉 2020-11-27 14:15

In Woocommerce, I am trying to get product custom attribute values but I fail miserably and I don\'t get anything.

So I tried:

global $woocommerce,          


        
相关标签:
9条回答
  • 2020-11-27 14:53

    The answer to "Any idea for getting all attributes at once?" question is just to call function with only product id:

    $array=get_post_meta($product->id);
    

    key is optional, see http://codex.wordpress.org/Function_Reference/get_post_meta

    0 讨论(0)
  • 2020-11-27 14:54

    Edited: The woocommerce_get_product_terms is deprecated since Woocommerce version 3

    Go with the following as @datafeedr wrote in his answer:

    global $product;
    $koostis = array_shift( wc_get_product_terms( $product->id, 'pa_koostis', array( 'fields' => 'names' ) ) );
    

    or even more compact:

    global $product;
    $koostis = $product->get_attribute( 'pa_koostis' );
    

    Original answer:

    $result = array_shift(woocommerce_get_product_terms($product->id, 'pa_koostis', 'names'));
    
    0 讨论(0)
  • 2020-11-27 14:56

    Most updated:

    $product->get_attribute( 'your_attr' );
    

    You will need to define $product if it's not on the page.

    0 讨论(0)
提交回复
热议问题