简体   繁体   English

WooCommerce使用WP_Query搜索价格范围之间的产品

[英]WooCommerce search products between price range using WP_Query

I am currently building my own custom advanced search functionality for a WordPress website with Woocommerce. 我目前正在使用Woocommerce为WordPress网站构建自己的自定义高级搜索功能。 You should be able to search using filter: 您应该可以使用过滤器进行搜索:

  • Category 类别
  • Min/Max price 最低/最高价格

My current progress is attached below. 我目前的进展情况附在下面。 This enables you to specify category slugs in the URL parameter. 这使您可以在URL参数中指定类别标签。 Returned will be posts that matches: 返回的帖子将匹配:

/**
 * Default arguments
 * @var array
 */

    $query = array(
        'post_status' => 'publish',
        'post_type' => 'product',
        'posts_per_page' => 10,
    );

/**
 * Category search
 */

    if(isset($_GET['categories']) && $_GET['categories']) {
        /**
         * Comma seperated --- explode
         * @var [type]
         */

            $categories = explode(',', $_GET['categories']);

        /**
         * Add "or" parameter
         */

            $query['tax_query']['relation'] = 'OR';

        /**
         * Add terms
         */

            foreach($categories as $category) {
                $query['tax_query'][] = array(
                        'taxonomy' => 'product_cat',
                        'field' => 'slug',
                        'terms' => $category,
                    );
            }
    }
/**
 * Fetch
 */

    $wp_query = new WP_Query($query);

Now, while this works great when you're searching for categories it seems it gets way more complicated when you need to search through prices. 现在,虽然这在您搜索类别时效果很好,但是当您需要搜索价格时,它似乎变得更加复杂。

In raw SQL, something like below would work: 在原始SQL中,类似下面的内容将起作用:

SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts
INNER JOIN $wpdb->postmeta ON ID = post_id
WHERE post_type IN ( 'product', 'product_variation' ) AND post_status = 'publish' AND meta_key = '_price' AND meta_value BETWEEN 200 AND 1000

I have no idea how I could implement this using WP_Query. 我不知道如何使用WP_Query实现它。

The solution, inspired by @Niels van Renselaar, but more clean: 该解决方案的灵感来自@Niels van Renselaar,但更加干净:

$query = array(
    'post_status' => 'publish',
    'post_type' => 'product',
    'posts_per_page' => 10,
    'meta_query' => array(
        array(
            'key' => '_price',
            'value' => array(50, 100),
            'compare' => 'BETWEEN',
            'type' => 'NUMERIC'
        )
    )
);

$wpquery = WP_Query($query); // return 10 products within the price range 50 - 100

You can combine multiple meta_queries while using WP_Query (or get_posts, which I found to be less intrusive in usage). 您可以在使用WP_Query(或get_posts,我发现使用起来不太麻烦)的同时合并多个meta_queries。

http://codex.wordpress.org/Class_Reference/WP_Meta_Query http://codex.wordpress.org/Class_Reference/WP_Meta_Query

http://codex.wordpress.org/Class_Reference/WP_Query http://codex.wordpress.org/Class_Reference/WP_Query

You can combine them by doing something like this 您可以通过执行以下操作将它们合并

$myposts = get_posts(
    array(
        'post_type' => array('product', 'product_variation'),
        'meta_query' => array(
            array(
                'key' => '_price',
                'value' => '200',
                'compare' => '>='
            ),
            array(
                'key' => '_price',
                'value' => '2000',
                'compare' => '<='
            )
        )
    )
);

If you was working inside woocommerce loop when you can use WC hook 如果您在woocommerce循环内工作,则可以使用WC挂钩

add_action( 'woocommerce_product_query', 'example_product_query_price' );

function example_product_query_price ( $q ) {
    $meta_query = $q->get( 'meta_query' );

    $meta_query[] = array(
        'key'     => '_regular_price',
        'value'   => array(
            300000 ,
            900000
        ),
        'compare' => 'BETWEEN',
        'type'=> 'NUMERIC'
    );

    $q->set( 'meta_query', $meta_query );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM