• Resolved jeff2mars

    (@jeff2mars)


    Hi

    I want to display only products according to a start date and an expiration date. When I only use one date in my function it works well, but when I also use the expiration date all the products are displayed even those out of date.

    here is my code in functions.php

    
    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;
    }

    Can you help me please?

    Thanks

Viewing 1 replies (of 1 total)
  • ValeDeOro

    (@valedeoro)

    Automattic Happiness Engineer

    I see that you already found the solution.

    Adding it here for posteriority:

    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;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘WooCommerce: display only products between two dates’ is closed to new replies.