Woocommerce:仅显示开始和结束日期之间的产品 [英] Woocommerce : only show products between start and end dates

查看:22
本文介绍了Woocommerce:仅显示开始和结束日期之间的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅显示两个日期之间的产品.当我只放置 1 个数组和一个日期时它可以工作,但是当我放置其他数组和我的第二个日期时它不再起作用,它显示所有产品.有什么想法吗?

I'm trying to display only products between two dates. When I only put 1 array and one date it works, but when I put other arrays and my second date it doesn't work anymore, it shows all the products. Any idea ?

function custom_meta_query( $meta_query ){
$today = current_time('Ymd');
 $args = array (
    'meta_query' => array(
'relation' => 'AND',
    array(
        'key'=>'flash_sale_start',
        'value' => $today,
        'compare'=>'<=',
    'type' => 'DATE'
    ),
       array(
    'key'=>'flash_sale_end',
        'value' => $today,
        'compare'=>'>=',
    'type' => 'DATE'
    )),);
$date_query = new WP_Query( $args );
    //return $meta_query;
}

// The main shop and archives meta query
add_filter( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 10, 2 );
function custom_product_query_meta_query( $meta_query, $query ) {
    if( ! is_admin() )
        return custom_meta_query( $meta_query );
}

// The shortcode products query
add_filter( 'woocommerce_shortcode_products_query', 'custom__shortcode_products_query', 10, 3 );
function custom__shortcode_products_query( $query_args, $atts, $loop_name ) {
    if( ! is_admin() )
        $query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
    return $query_args;
}

// The widget products query
add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
    if( ! is_admin() )
        $query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
    return $query_args;
}

谢谢

推荐答案

我找到了解决方案,这是一个数组()问题

I've found the solution, it was an array() problem

function custom_meta_query( $meta_query ){
$today = current_time('Ymd');
 $args = array (
    'numberposts' => -1,
    'meta_query' => array(
    'relation' => 'AND',
    'start_clause' => array(
        'key'=>'flash_sale_start',
        'value' => $today,
        'compare'=> '<=',
     'type' => 'DATE'
    ),
         'end_clause' => array(
         'key' => 'flash_sale_end',
     'value' => $today,
         'compare' => '>=',
     'type' => 'DATE'
     ),
       ));

return $args;
}

// The main shop and archives meta query
add_filter( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 10, 2 );
function custom_product_query_meta_query( $meta_query, $query ) {
    if( ! is_admin() )
        return custom_meta_query( $meta_query );
}

// The shortcode products query
add_filter( 'woocommerce_shortcode_products_query', 'custom__shortcode_products_query', 10, 3 );
function custom__shortcode_products_query( $query_args, $atts, $loop_name ) {
    if( ! is_admin() )
        $query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
    return $query_args;
}

// The widget products query
add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
    if( ! is_admin() )
        $query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
    return $query_args;
}

这篇关于Woocommerce:仅显示开始和结束日期之间的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆