2017-08-16 11 views
1

私は、カスタム製品ページでバリエーション商品のバリエーションを取得しようとしています。私は二つの属性を持っています。ひとつはサイズ選択用、もう一つは色見本用です。私が表示する必要がある属性を表示することができないという問題、および次のコードを使用すると、サイズまたは色の選択ドロップダウンではなくサイズまたは色のテキスト名が返されます。助けてください!Woocommerceバリエーション属性を取得

echo implode(', ', wc_get_product_terms($product_id, 'pa_colors')); 

答えて

2

これはあなたの質問を解決するための簡単なコードです。私はあなたに必要なものだけを使用できます。

最初に、get_product機能が存在するかどうかをチェックし、製品タイプを確認して、id(私の場合は$idProduct)で正しい製品オブジェクトを作成します。

woocommerce 3.xで動作しますが、私はwoocommerceでテストしません< 3.x.

if(function_exists('get_product')) { 
     $product = get_product($idProduct); 
     if ($product->is_type('variable')) { 

      $product = new WC_Product_Variable($idProduct); 

      $available_variations = $product->get_available_variations(); //get all child variations 
      $variation_variations = $product- >get_variation_attributes(); // get all attributes by variations 

      // taxonomy   => terms 
      // pa_attribute-color => array('blue', 'red', green) 
      // Use ex: get_taxonomy('pa_attribute-color')->labels; to get the Name and not the slug to attributes, it can be the taxonomy 
      // Use ex: get_term_by('name', 'pa_attribute-color', 'pa_attribute-color); to get the Name/label 

      $result = array($available_variations , $attributes); // only to see the result you can use var_dump, error_log, etc. 
      //... 
      //... 
     }elseif ($product->is_type('bundle') && class_exists('WC_Product_Bundle')) { 
      $product = new WC_Product_Bundle($idProduct); 
     }else{ 
      $product = new WC_Product($idProduct); 
     } 
    } 

また、あなたがしてみてください:

$keyはなど

pa_colorpa_size、することができ

$product->get_attribute($key); 
wc_attribute_label($key); 

を私はあなたを助け願っています。

関連する問題