2016-04-07 3 views
0

Ich habe einen WooCommerce-Shop, der variable Produkte enthält.WooCommerce Alle Produkte mit Variationen anzeigen

Ich möchte eine Art Export (auf einer Seite) erstellen, die mir alle Produkte einschließlich Variationen in einer Tabelle zeigt.

Ich sehe alle Produkte und erstellt eine Schleife, um die Variationen zu erhalten, aber es zeigt mir nur eine Produktvariante.

<?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; 

?> 

Kann mir jemand sagen, wie man alle Variationen für alle Produkte bekommt?

M.

Antwort

1

Zuerst überschreiben Sie nicht die Produktvariable innerhalb der Schleife. Zweitens müssen Sie überprüfen, ob das Wetterprodukt einfach oder variabel ist. Weil einfaches Produkt keine Varianten hat. So wird Ihr Code wie folgt aussehen:

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

Vielen Dank. Mein aktuelles Problem ist jedoch, dass es nur ein Produkt zeigt und nicht die anderen ... Irgendwelche Gedanken? – Interactive

+0

Es gefunden. Ich habe die 'Die;' am Ende entfernt, damit die Schleife nicht enden würde. Ich danke dir sehr!!! – Interactive

+1

Ich habe festgestellt, dass das '$ variations' Array ein anderes Array enthält. Soll ich eine andere Schleife benutzen? Das ist das Array: '[0] => Array ([Attribute] => Array ([Attribut_Pa_Größen] => Smallmedium [Attribut_Pa_Farbe] => Grün) [Image_Src] =>' – Interactive