Запрос функции wc_get_products для получения товаров, если значение атрибута находится в диапазоне - PullRequest
1 голос
/ 03 мая 2020

У меня есть атрибут продукта со слагом height
Я хочу получить все продукты, где height находится в диапазоне чисел c. Этот запрос не работает:

array(
  'status' => 'publish',
  'paginate' => true,
  'limit' => $limit,
  'offset' => $offset,
  'attributes' => array(
                    array(
                      'field'   => 'slug',
                      'name'    => 'height',
                      'value'   => array( $min_value, $max_value ),
                      'compare' => 'BETWEEN',
                      'type'    => 'NUMERIC'
                    )
                  )
  );

array(
  'status' => 'publish',
  'paginate' => true,
  'limit' => $limit,
  'offset' => $offset,
  'tax_query' => array(
                    array(
                      'taxonomy'=> 'pa_height',
                      'name'    => 'height',
                      'value'   => array( $min_value, $max_value ),
                      'compare' => 'BETWEEN',
                      'type'    => 'NUMERIC'
                    )
                  )
  );

Есть ли способ запроса по диапазону значений атрибута?

1 Ответ

0 голосов
/ 03 мая 2020

WP_Tax_Query примет только 'term_id' или 'slug' для параметра fields

/**
 * Constructor.
 *
 * @since 3.1.0
 * @since 4.1.0 Added support for `$operator` 'NOT EXISTS' and 'EXISTS' values.
 *
 * @param array $tax_query {
 *     Array of taxonomy query clauses.
 *
 *     @type string $relation Optional. The MySQL keyword used to join
 *                            the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'.
 *     @type array {
 *         Optional. An array of first-order clause parameters, or another fully-formed tax query.
 *
 *         @type string           $taxonomy         Taxonomy being queried. Optional when field=term_taxonomy_id.
 *         @type string|int|array $terms            Term or terms to filter by.
 *         @type string           $field            Field to match $terms against. Accepts 'term_id', 'slug',
 *                                                 'name', or 'term_taxonomy_id'. Default: 'term_id'.
 *         @type string           $operator         MySQL operator to be used with $terms in the WHERE clause.
 *                                                  Accepts 'AND', 'IN', 'NOT IN', 'EXISTS', 'NOT EXISTS'.
 *                                                  Default: 'IN'.
 *         @type bool             $include_children Optional. Whether to include child terms.
 *                                                  Requires a $taxonomy. Default: true.
 *     }
 * }
 */

Возможно, вам потребуется получить продукты с этим атрибутом, а l oop - результат для значение между.

...