Recuperare prodotti con attributo specifico e in categoria - WooCommerce

7 lug 2017, 13:08:08
Visualizzazioni: 26.6K
Voti: 2

Sto cercando di ottenere un elenco/conteggio dei prodotti che hanno uno specifico attributo e valore. I prodotti sono gestiti e configurati utilizzando il plugin WooCommerce.

Ogni prodotto ha lo stesso set di variazioni, il prodotto è assegnato a una categoria, ho bisogno di recuperare solo i prodotti con un attributo specifico come "newyork" e la quantità di stock inferiore a "20".

Ogni variazione del prodotto è impostata su Gestione stock, ma non il prodotto stesso, spero che abbia senso. Il mio problema al momento è che la query WordPress che ho non controlla affatto il nome della variazione o lo stock.

Qualsiasi aiuto sarebbe apprezzato.

<?php 

$args = array(
  'post_type'           => array('product', 'product_variation'),
  'post_status'         => 'publish',
  'posts_per_page'      => -1,
  'meta_query'          => array(
    array(
      'key'         => '_stock',
      'value'       => 20,
      'compare'     => '<'
    )
  ),
  'tax_query'           => array(
    array(
      'taxonomy'        => 'product_cat',
      'field'           => 'slug',
      'terms'           => array('cat1', 'cat2', 'cat3'),
      'operator'        => 'IN',
    ),
    array(
      'taxonomy'        => 'pa_regions',
      'field'           => 'slug', 
      'terms'           => 'newyork',
      'operator'        => 'IN'
    ),
  )
);

$loop = new WP_Query($args);
$post_count = array();

while($loop->have_posts()) : $loop->the_post();
  global $product; ?>

  <pre style="background: #1c1c1b; color: #f7e700">
    <?php $post_count[] = $product->ID; ?>
  </pre>

<?php endwhile; ?>

$total = count($post_count);
0
Tutte le risposte alla domanda 2
0

Dovresti usare wc_get_products con un filtro personalizzato per aggiungere la tua query specifica.

Esempio

Voglio trovare prodotti che contengono un valore attributo specifico "table-filter".

  $args = array(
            'table-filter' => array(1,2,3)
        );
  $products = wc_get_products($args);

Poi ho un filtro per questo:

add_filter('woocommerce_product_data_store_cpt_get_products_query', 'my_handle_custom_query_var', 10, 2);

function my_handle_custom_query_var($query, $query_vars) {
    if (!empty($query_vars['table-filter'])) {
        $query['tax_query'][] = array(
            'taxonomy' => 'pa_table-filter',
            'field'    => 'term_id', //default
            'terms'    => $query_vars['table-filter'],
            'operator' => 'IN',
        );
    }
    return $query;
}

Suggerimento: La tassonomia dell'attributo generata da WooCommerce è sempre prefissata con "pa_", quindi se il tuo slug attributo è "table-filter", la tassonomia sarà "pa_table-filter".

26 apr 2018 10:11:41
0
-1

Penso tu abbia un problema con 'tax_query'. Se vuoi usare l'array di query in 'tax_query', devi seguire il formato WordPress

Ecco una breve spiegazione per te: in 'tax_query' è necessario specificare 'relation':

 'tax_query'           => array(
   'relation'=>'AND', // 'AND' 'OR' ...
    array(
      'taxonomy'        => 'product_cat',
      'field'           => 'slug',
      'terms'           => array('cat1', 'cat2', 'cat3'),
      'operator'        => 'IN',
    ),
    array(
      'taxonomy'        => 'pa_regions',
      'field'           => 'slug',
      'terms'           => 'newyork',
      'operator'        => 'IN'
    )
24 gen 2018 06:07:28