2016-04-07 10 views
0

私は可変商品を保持するWooCommerceショップを持っています。WooCommerceバリエーションのあるすべての商品を表示

テーブル内のバリエーションを含むすべての商品を表示するエクスポートを作成したいと考えています。

私はすべての製品を表示していますが、バリエーションを得るためにループを作成しましたが、1つの製品バリエーションしか表示されません。

<?php 
/* 
Template Name: Store Management 
*/ 
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_die('This page is private.'); 
// Get 
$args = array(
    'post_type' => 'product', 
    'numberposts' => -1, 
); 
$products = get_posts($args); 
echo '<pre>'; 
print_r($products); 
echo '</pre>'; 

foreach($products as $product): 

    $args = array(
     'post_parent' => $plan->ID, 
     'post_type' => 'product_variation', 
     'numberposts' => -1, 
    ); 

    $product = wc_get_product($product->ID); 

    $variations = $product->get_available_variations(); 
    echo '<pre>'; 
    print_r($variations); 
    echo '</pre>'; 

endforeach; 

?> 

すべてのバリエーションをすべての製品に適用する方法を教えてもらえますか?

M.

答えて

1

まずループ内の製品の変数を上書きしません。第二に、気象製品が単純か可変かをチェックする必要があります。シンプルな製品にはバリエーションがないためです。コードは次のようになります。

foreach($products as $product): 
$product_s = wc_get_product($product->ID); 
if ($product_s->product_type == 'variable') { 
    $args = array(
     'post_parent' => $plan->ID, 
     'post_type' => 'product_variation', 
     'numberposts' => -1, 
    ); 
    $variations = $product_s->get_available_variations(); 
    echo '<pre>'; 
    print_r($variations); 
    echo '</pre>'; 
} 
endforeach; 
+0

ありがとうございます。しかし、私の現在の問題は、それは1つの製品だけを表示し、他の製品は表示しないということです。 – Interactive

+0

見つけました。最後にループが終了しないように 'die ;;'を削除しました。どうもありがとうございます!!! – Interactive

+1

'$ variations'配列は別の配列を保持しています。別のループを使うべきですか?これは配列です: '[0] => Array([attribute] => Array([attribute_pa_sizes] => smallmedium [attribute_pa_color] => green)[image_src] => – Interactive

関連する問題