2016-07-20 26 views
4

woocommerceで特定のカスタム属性を取得しようとしています。 私はこのサイトで約3〜5の方法を提供するスレッドのトンを読んだことがあります。 すべてを試した後、私のために働いた唯一の方法は、すべての属性をループすることです。他はすべて機能しませんでした。 私は 'PDFファイル'WooCommerce - カスタム商品属性を取得する

次の試行が機能しなかったという名前のカスタム属性を持っている:を(link

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

$myPdf = $product->get_attribute('pdfs'); 

$myPdf = get_post_meta($product->id, 'pdfs', true); 

これは仕事をした唯一のものです:link

$attributes = $product->get_attributes(); 
foreach ($attributes as $attribute) { 
    if (attribute_label($attribute[ 'name' ]) == "pdfs") { 
     echo array_shift(wc_get_product_terms($product->id, $attribute[ 'name' ])); 
    } 
} 

私は多くの場合、最初のオプションの1つを使用することができます 助けていただければ幸いです。
おかげ

答えて

7

更新:Woocommerce 3+のための追加された互換性

属性は常にDBにpa_で先頭に追加されると、wc_get_product_terms()機能でそれらを取得するために、あなたはを使用する必要がありますpa_pdfspdfsの代わりに、

global $product; 

$product_id = method_exists($product, 'get_id') ? $product->get_id() : $product->id; // Added WC 3+ support 

$myPdf = array_shift(wc_get_product_terms($product_id, 'pa_pdfs', array('fields' => 'names'))); 

参考:How to get a products custom attributes from WooCommerce

+0

パーフェクト - ありがとう! – DaveyD

+0

あなたの答えをありがとう、本当に役に立ちました。 –

+0

私は取得します - >通知:変数のみが参照渡しされる必要があります。 Woocommerceバージョン3.2.6コード---> $ date = array_shift(wc_get_product_terms($ product-> get_id()、 'pa_date'、array( 'fields' => 'names')));何が間違っていますか?私はそれを解決することはできません。私はgettype($ date)の 'Null'を取得します。多分私はいくつかの助けを得ることができますか? – Kristis

関連する問題