我如何通过 00 美元产品的定价查询 WooCommerce 产品?

How can i query WooCommerce product by pricing of $00 product?

我的模板中有一个自定义查询,我想按价格显示产品,我想查询免费产品,并且在我的主页上只显示 $0 产品。

将此添加到您的主题 functions.php,如果有效请告诉我。

add_action( 'woocommerce_product_query', 'zero_price_products' );
function zero_price_products( $q ){
    $meta_query = $q->get( 'meta_query' );
        $meta_query[] = array(
        'key'       => '_price',
        'value'     => 0,
        'compare'   => '='
    );
    $q->set( 'meta_query', $meta_query );
}

如果你想在你的主页上显示产品添加此代码:

<?php
$args = array(
    'post_type' => 'product',
    'posts_per_page' => 8,
    'post_status' => 'publish',
    'orderby' => 'id',
    'order' => 'DESC',
    'meta_query' => array(
           array(
           'key'       => '_price',
           'compare'   => '=',
           'value'      => 0,
           )
        ),
);


$custom_posts = new WP_Query( $args );

if ($custom_posts->have_posts() ) : while ($custom_posts->have_posts() ) : $custom_posts->the_post(); ?>
    <?php
        wc_get_template_part( 'content', 'product' );
     ?>
<?php endwhile; endif; ?>
$args = array(
            'post_type'      => 'product',
            'numberposts'      => -1,
            'meta_query'     => array(
                'relation' => 'OR',
                array(
                    'key'           => '_regular_price',
                    'compare' => 'NOT EXISTS'
                ),
                array( 
                    'key'           => '_price',
                    'compare' => 'NOT EXISTS'
                    
                ),
                array( 
                    'key'           => '_regular_price',
                    'compare' => '=',
                    'value' => 0,
                ),
                array( 
                    'key'           => '_price',
                    'compare' => '=',
                    'value' => 0,
                )
            )
        );
        
        $products =  get_posts( $args );