Shortcode of scheduled sales products
-
What’s the shortcode of showing only schedule sales products? or is there any way to show only scheduled sales products?
-
@rifatspir Sorry, You’re right
Try this one:
add_shortcode( 'ywp_schedule_product', 'ywp_schedule_product_availability_shortcode' ); function ywp_schedule_product_availability_shortcode($atts) { global $post; $time=time(); extract( shortcode_atts( array( 'limit' => '', 'columns'=>'' ), $atts) ); $columns = ! empty( $columns ) ? $columns : 3; $limit = ! empty( $limit ) ? $limit : 12; $args = array( 'post_type' => array('product', 'product_variation'), 'post__in' => array_merge( array( 0 ), wc_get_product_ids_on_sale() ), 'posts_per_page' => $limit, 'meta_query' => array( /*'relation' => 'AND', // If you set on sale start date uncomment this section array( 'key' => '_sale_price_dates_from', 'value' => $time, 'compare' => '<', ),*/ array( 'key' => '_sale_price_dates_to', 'value' => $time, 'compare' => '>', ), ), ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { ?> <ul class ="products columns-<?php echo $columns; ?>"> <?php while ( $loop->have_posts() ) : $loop->the_post(); woocommerce_get_template_part( 'content', 'product' ); endwhile; ?> </ul> <?php } else echo __( 'No products found' ); wp_reset_postdata(); }
- This reply was modified 4 years, 8 months ago by Hamid Reza Yazdani.
Great its showing variable products now.
As there are lot of items, i need to add pagination, any idea how do i add it?
add_shortcode( 'ywp_schedule_product', 'ywp_schedule_product_availability_shortcode' ); function ywp_schedule_product_availability_shortcode($atts) { global $post; $time=time(); extract( shortcode_atts( array( 'limit' => '', 'columns'=>'' ), $atts) ); $columns = ! empty( $columns ) ? $columns : 3; $limit = ! empty( $limit ) ? $limit : 12; $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $args = array( 'post_type' => array('product', 'product_variation'), 'post__in' => array_merge( array( 0 ), wc_get_product_ids_on_sale() ), 'paged' => $paged, 'posts_per_page' => $limit, 'meta_query' => array( /*'relation' => 'AND', // If you set on sale start date uncomment this section array( 'key' => '_sale_price_dates_from', 'value' => $time, 'compare' => '<', ),*/ array( 'key' => '_sale_price_dates_to', 'value' => $time, 'compare' => '>', ), ), ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { ?> <ul class ="products columns-<?php echo $columns; ?>"> <?php while ( $loop->have_posts() ) : $loop->the_post(); woocommerce_get_template_part( 'content', 'product' ); endwhile; ?> </ul> <?php if ( function_exists( 'pagination' ) ) pagination( $loop->max_num_pages ); } else echo __( 'No products found' ); wp_reset_postdata(); }
@yazdaniwp Tried that, pagination not working.
add_shortcode( 'ywp_schedule_product', 'ywp_schedule_product_availability_shortcode' ); function ywp_schedule_product_availability_shortcode($atts) { global $post; $time=time(); extract( shortcode_atts( array( 'limit' => '', 'columns'=>'' ), $atts) ); $columns = ! empty( $columns ) ? $columns : 3; $limit = ! empty( $limit ) ? $limit : 12; $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $args = array( 'post_type' => array('product', 'product_variation'), 'post__in' => array_merge( array( 0 ), wc_get_product_ids_on_sale() ), 'paged' => $paged, 'posts_per_page' => $limit, 'meta_query' => array( /*'relation' => 'AND', // If you set on sale start date uncomment this section array( 'key' => '_sale_price_dates_from', 'value' => $time, 'compare' => '<', ),*/ array( 'key' => '_sale_price_dates_to', 'value' => $time, 'compare' => '>', ), ), ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { ?> <ul class ="products columns-<?php echo $columns; ?>"> <?php while ( $loop->have_posts() ) : $loop->the_post(); woocommerce_get_template_part( 'content', 'product' ); endwhile; ?> </ul> <div class="pagination"> <?php echo paginate_links( array( 'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ), 'total' => $loop->max_num_pages, 'current' => $paged, 'format' => '?paged=%#%', 'show_all' => false, 'type' => 'plain', 'end_size' => 2, 'mid_size' => 1, 'prev_next' => true, 'prev_text' => sprintf( '<i></i> %1$s', 'Prev' ), 'next_text' => sprintf( '%1$s <i></i>', 'Next' ), 'add_args' => false, 'add_fragment' => '', ) ); ?> </div> <?php } else echo __( 'No products found' ); wp_reset_postdata(); }
@yazdaniwp Now working and this is what i wanted. Only the columns number not working. It follows my theme default product columns number.
Thank you for your help ??
You’re welcome
The number of columns is related to your site’s css and you need to change it
Good luck
- The topic ‘Shortcode of scheduled sales products’ is closed to new replies.