WooCommerce – Recent Products Offset

Background: WooCommerce provides a shortcode to display recent products any place I want.

  <?php echo do_shortcode('[recent_products columns="3"]'); ?>

There is an argument in WP_Query named offset that allows us to pass over desired number posts.

Read More
  <?php $query = new WP_Query( array( 'offset' => 3 ) ); ?>

So, if I use the above query to loop over posts, the first result I’d get would be the fourth latest post. Right?

Question: I was wondering if it would be possible to extend WC’s Recent Posts shortcode to accept offset argument?

Related posts

1 comment

  1. You’ll have to change in wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php the recent_products() method like that:

    public static function recent_products( $atts ) {
            $atts = shortcode_atts( array(
                'per_page' => '12',
                'columns'  => '4',
                'orderby'  => 'date',
                'order'    => 'desc',
                'offset'   => 0,
                'category' => '',  // Slugs
                'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
            ), $atts );
            $query_args = array(
                'post_type'           => 'product',
                'post_status'         => 'publish',
                'ignore_sticky_posts' => 1,
                'posts_per_page'      => $atts['per_page'],
                'orderby'             => $atts['orderby'],
                'order'               => $atts['order'],
                'offset'              => $atts['offset'],
                'meta_query'          => WC()->query->get_meta_query()
            );
            $query_args = self::_maybe_add_category_args( $query_args, $atts['category'], $atts['operator'] );
            return self::product_loop( $query_args, $atts, 'recent_products' );
        }
    

    With this an offset attribute is added (default 0) that will be used in WP_Query.

Comments are closed.