gpt4 book ai didi

wordpress - WooCommerce REST API : query products by multiple attributes

转载 作者:行者123 更新时间:2023-12-05 09:17:00 25 4
gpt4 key购买 nike

根据 API:http://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products我们可以按单个属性过滤产品。但是是不是不能通过API进行多属性搜索呢?

示例:“我想要红色 衬衫”。这里的属性是color,属性术语是red。要完成搜索,查询字符串如下所示:products?category=17&attribute=pa_color&attribute_term=22&

而且我们只得到红色衬衫。

但对于“我想要red medium shirts”,这里有一个额外的size属性,值为medium遭遇。根据 API,无法在查询字符串中关联 colorsize 属性。所以查询 -

products?category=17&attribute=pa_color&attribute_term=22&attribute=pa_size&attribute_term=24& 

从商店退回所有产品

有什么解决方法吗?

最佳答案

我已经编写了可以完成这一切的自定义查询:D这是一个回调函数。您需要做的就是使用此回调创建新端点:

这是一个文档:

category - string (category slug) - filter by products category.
per_page - int (default - from admin) - show items on one page.
offset - int (default - 1) - show page number.
order - string (ASC/DESC, default: desc) - order products ascending or descending.
orderby - string (name, price, default: name) - order products by key.
filter - array
[pa_attribute_name (string) ] = array of ID's.
[min_price (string) ] = int.
[max_price (string) ] = int.

示例:

/wp-json/go/v1/products/?category=smartphones&filter[pa_brand]=87,88&filter[pa_colour]=17&filter[min_price]=1&filter[max_price]=50&per_page=10&offset=1&order=DESC&orderby=price

这是一个函数:

public function get_products_list_callback( \WP_REST_Request $request ) {
$params = $request->get_params();

$category = General_Helper::get_array_value( 'category', $params );
$filters = General_Helper::get_array_value( 'filter', $params );
$per_page = General_Helper::get_array_value( 'per_page', $params );
$offset = General_Helper::get_array_value( 'offset', $params );
$order = General_Helper::get_array_value( 'order', $params );
$orderby = General_Helper::get_array_value( 'orderby', $params );

$output = [];

// Use default arguments.
$args = [
'post_type' => Config::POST_TYPE_SLUG_PRODUCT,
'posts_per_page' => get_option( 'posts_per_page' ),
'post_status' => 'publish',
'paged' => 1,
];

// Posts per page.
if ( ! empty( $per_page ) ) {
$args['posts_per_page'] = $per_page;
}

// Pagination, starts from 1.
if ( ! empty( $offset ) ) {
$args['paged'] = $offset;
}

// Order condition. ASC/DESC.
if ( ! empty( $order ) ) {
$args['order'] = $order;
}

// Orderby condition. Name/Price.
if ( ! empty( $orderby ) ) {
if ( $orderby === 'price' ) {
$args['orderby'] = 'meta_value_num';
} else {
$args['orderby'] = $orderby;
}
}

// If filter buy category or attributes.
if ( ! empty( $category ) || ! empty( $filters ) ) {
$args['tax_query']['relation'] = 'AND';

// Category filter.
if ( ! empty( $category ) ) {
$args['tax_query'][] = [
'taxonomy' => Config::TAXONOMY_SLUG_PRODUCT,
'field' => 'slug',
'terms' => [ $category ],
];
}

// Attributes filter.
if ( ! empty( $filters ) ) {
foreach ( $filters as $filter_key => $filter_value ) {
if ( $filter_key === 'min_price' || $filter_key === 'max_price' ) {
continue;
}

$args['tax_query'][] = [
'taxonomy' => $filter_key,
'field' => 'term_id',
'terms' => \explode( ',', $filter_value ),
];
}
}

// Min / Max price filter.
if ( isset( $filters['min_price'] ) || isset( $filters['max_price'] ) ) {
$price_request = [];

if ( isset( $filters['min_price'] ) ) {
$price_request['min_price'] = $filters['min_price'];
}

if ( isset( $filters['max_price'] ) ) {
$price_request['max_price'] = $filters['max_price'];
}

$args['meta_query'][] = \wc_get_min_max_price_meta_query( $price_request );
}
}

$the_query = new \WP_Query( $args );

if ( ! $the_query->have_posts() ) {
return $output;
}

while ( $the_query->have_posts() ) {
$the_query->the_post();

$output[] = get_the_title();
}
wp_reset_postdata();

return $output;
}

希望对你有帮助

关于wordpress - WooCommerce REST API : query products by multiple attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49006417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com