2017-02-23 16 views
2

特定の属性データとともに特定のWooCommerce変数サブスクリプション属性を表示しようとすると、variable-subscription.phpファイルを編集しています。これまでのコードは、バリエーションイメージと説明とともに属性名を表示しようとしています。現在バリエーションの特定の属性に対応するデータ値を取得します

<?php 

// get all variations 
$variations = $product->get_available_variations(); 
// get shirt values 
$shirt_values = get_the_terms($product_id, 'pa_shirt'); 
// for each shirt value 
foreach ($shirt_values as $shirt_value) { ?> 

<h1> 
    <?php // name 
     echo echo $shirt_value->name; 
    ?> 
</h1> 

<?php // description 
    foreach ($variations as $variable_array){ 
     $variation = new WC_Product_Variation($variable_array['variation_id']); 
     echo $variation->variation_description; 
    } 
?> 

<?php // image 
    foreach ($variations as $variation) { ?> 
     <img src="<?php echo $variation['image_src']; ?>"> 
    <?php } 
?> 

、私は(カスタム製品の属性で定義された)販売のための3枚のシャツを持っているし、私はいくつかのバリエーションがあります。問題は、このPHPが各シャツのすべての可能な説明/画像のバリエーションを示していることです。私はそれぞれのシャツの属性に関連するバリエーションを表示したいだけです。

アドバイスは非常に高く評価されます。

答えて

1

WC_Product_Variationクラスでメソッドget_variation_attributes()を使用する必要があります。このメソッドは、属性の配列のバリエーションを返します。あなたは、アレイ内の属性キーのナメクジは、すべての'attribute_'を持って気づくことができたよう

$variations = $product->get_available_variations(); 

// Here we use our method to get the array of attribute values for the variation 
$variation_attribute = $variation->get_variation_attributes(); 

// Here is the value for this variation and "pa_shirt" attribute 
$variation_attrib_shirt_value = $variation_attribute['attribute_pa_shirt']; 

だから属性pa_shirtのために特定のバリエーションの値を表示するには、あなたのコードがあることを行っています最初は

+0

ありがとうございました!私は正解とマークした。 –

関連する問題