2017-01-25 3 views
0

Ich habe eine benutzerdefinierte Abfrage erstellt, um die Produktvariationen zu erhalten, aber wenn ich ein Produkt trenne, bleibt der Variationsstatus publish, daher wird ein 404 Fehler beim Kunden angezeigt versucht, die verworfenen Produktvariationen zu sehen. Also, wie kann ich diese Variationen filtern, um nur die Variationen zu erhalten, die das Elternprodukt veröffentlicht haben?So erhalten Sie Produktvariationen nur, wenn das übergeordnete Produkt veröffentlicht wird

Mein Code:

<?php 
$args = ['post_type' => ['product_variation'], 
      'orderby' => 'meta_value_num', 
      'order'  => 'DESC', 
      'post_status'=>'publish', 
      'product_type'=>'variation', 
      'meta_query' => [ 
       [ 
        'key'  => 'attribute_pa_flower-type', 
        'value' => $flower_type, 
        'compare' => '=', 
       ] 
      ] 
     ]; 
?> 

<?php $the_query = new WP_Query($args);?> 

<?php if ($the_query->have_posts()) : ?> 

    <div class="boxes"> 
     <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
     ... 

Antwort

1

Tun Sie etwas, was wie folgt aus:

//to hold the published product id which has vriation. 
$has_variable_ids = []; 

$args_parent = [ 
    'post_type' => ['product'], 
    'post_status' => 'publish', 
    'posts_per_page' => -1 
]; 

$pubished_post = new WP_Query($args_parent); 
if (!empty($pubished_post->posts)) 
{ 
    foreach ($pubished_post->posts as $post) 
    { 
     $_product = wc_get_product($post->ID); 
     if ($_product->is_type('variable')) 
     { 
      // Product has variations 
      $has_variable_ids[] = $post->ID; 
     } 
    } 
} 


$args = [ 
    'post_type' => ['product_variation'], 
    'orderby' => 'meta_value_num', 
    'order' => 'DESC', 
    'post_status' => 'publish', 
    'post_parent__in' => $has_variable_ids, 
    'meta_query' => [ 
     [ 
      'key' => 'attribute_pa_flower-type', 
      'value' => $flower_type, 
      'compare' => '=', 
     ] 
    ] 
]; 

$the_query = new WP_Query($args); 

Bitte beachten Sie: Ich habe es getestet have't, aber es sollte funktionieren.

Hoffe, das hilft!

+0

yeah das ist funktioniert, was ich schließlich ist, um den Elternbeitrag für jede Variation zu bekommen und seinen Status innerhalb der While-Schleife zu überprüfen, aber ich werde meinen Code ändern, weil es schneller – Mohammad

+0

Ich habe nicht gefunden und WooCommerce-Funktion Aus diesem Grund habe ich mein WP-Wissen gebildet. –

Verwandte Themen