skip to Main Content

I want to create a Woocommerce shop page that only displays products published last month. What is the best way to accomplish this? I’ve tried several plugins without succes. From my understanding you can accomplish this with standard Woocommerce, Woocommerce blocks and some shortcode.

Any suggestions are appreciated.

3

Answers


  1. Chosen as BEST ANSWER

    Solution here: https://www.sitepoint.com/community/t/show-latest-30-days-products-in-woocommerce/258276

    function baba_recent_products() {
        //return 'This is where the recent products should show up if I get the shortcode working. ';
        
        
        global $woocommerce_loop;
    
            extract( shortcode_atts( array(
                'per_page'  => '48',
                'columns'   => '2',
                'orderby'   => 'date',
                'order'     => 'desc'
            ), $atts ) );
    
            $meta_query = WC()->query->get_meta_query();
    
            $args = array(
                'post_type'             => 'product',               
                'post_status'           => 'publish',
                'ignore_sticky_posts'   => 1,
                'posts_per_page'        => $per_page,
                'orderby'               => $orderby,
                'order'                 => $order,
                'meta_query'            => $meta_query
            );
    
            ob_start();
    
            $products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
    
            $woocommerce_loop['columns'] = $columns;
    
            if ( $products->have_posts() ) : ?>
    
                <?php woocommerce_product_loop_start(); ?>
    
                    <?php while ( $products->have_posts() ) : $products->the_post(); ?>
    
                        <?php wc_get_template_part( 'content', 'product' ); ?>
    
                    <?php endwhile; // end of the loop. ?>
    
                <?php woocommerce_product_loop_end(); ?>
    
            <?php endif;
    
            wp_reset_postdata();
    
            return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
        }
        
        
        add_shortcode( 'baba_recent_products', 'baba_recent_products' );
    
    function filter_where($where = '') {
        //posts in the last 30 days
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
        return $where;
    }
    add_filter('posts_where', 'filter_where');
    

  2. I just made a shortcode for you. Which will return all products from last month. you can use this shortcode anywhere: [wc_get_products_last_month]

    add_shortcode('wc_get_products_last_month', function( $atts, $content = null ){
    
        $start_date = array(
            'year'  => date("Y", strtotime("first day of previous month")),
            'month' => date("n", strtotime("first day of previous month")),
            'day'   => date("j", strtotime("first day of previous month"))
        );
    
        $end_date = array(
            'year'  => date("Y", strtotime("last day of previous month")),
            'month' => date("n", strtotime("last day of previous month")),
            'day'   => date("j", strtotime("last day of previous month"))
        );
    
    
        $args = array(
            'post_type' => 'product',
            'date_query' => array(
                array(
                    'after'     => $start_date,
                    'before'    => $end_date,
                    'inclusive' => true,
                ),
            ),
            'posts_per_page' => -1,
        );
    
        $query = new WP_Query( $args );
    
        if ( $query->have_posts() ) {
    
            while( $query->have_posts() ) { $query->the_post();
                // Get $product object from product ID
                $product = wc_get_product( get_the_ID() );
    
                // Do you stuff here
                //_e( $product->get_name() );
    
            }
    
        } else {
            echo 'No Posts';
        }
    
        wp_reset_query();
    });
    

    Tested and Worked.

    Login or Signup to reply.
  3. Try this. Combination of these two answers (credit goes to them. I am just updating code by your requirements). put this code in your active theme functions.php file.

    https://stackoverflow.com/a/66528155/6469645

    Woocommerce : only show products between start and end dates

    function custom_meta_query( $meta_query ){
    
        $start_date = array(
            'year'  => date("Y", strtotime("first day of previous month")),
            'month' => date("n", strtotime("first day of previous month")),
            'day'   => date("j", strtotime("first day of previous month"))
        );
    
        $end_date = array(
            'year'  => date("Y", strtotime("last day of previous month")),
            'month' => date("n", strtotime("last day of previous month")),
            'day'   => date("j", strtotime("last day of previous month"))
        );
    
        $args = array(
            'post_type' => 'product',
            'post_status' => 'publish',
            'date_query' => array(
                array(
                    'after'     => $start_date,
                    'before'    => $end_date,
                    'inclusive' => true,
                ),
            ),
            'posts_per_page' => -1,
        );
    
        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;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search