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?
-
I’m not sure what you mean by “schedule sale products,” but the shortcode to display products that are on sale is:
[products on_sale]
You can combine that with all the other products attributes as described in the Documentation.
The current way to do this is to use Blocks.
Hi
Try to add this snippet to your active theme’s/child theme’s functions.php file:
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( $atts['columns'] ) ? $atts['columns'] : 3; $args = array( 'post_type' => 'product', 'post__in' => implode( ',', get_on_sale_products() ) 'posts_per_page' => $atts['limit'], 'meta_query' => array( 'relation' => 'AND', array( 'key' => '_sale_price_dates_from', 'value' => current_time( 'timestamp', 1 ), 'compare' => '>', ), array( 'key' => '_sale_price_dates_to', 'value' => current_time( 'timestamp', 1 ), '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(); }
Usage:
[ywp_schedule_product limit=12 column=3]
Good luck
@yazdaniwp, thanks for understand. There is a problem with this code, CRITICAL Uncaught Error: Call to undefined function get_on_sale_products()
'post__in' => implode( ',', get_on_sale_products() )
I think to call that function, you’d have to make the WooCommerce functions global. You might try the current, wc version, which shouldn’t require a global:
'post__in' => implode( ',', wc_get_product_ids_on_sale() )
@linux4me2 yes did that…. it works but shows “No products found”. Looks like it cannot get schedule sales item. I have more than 30+ schedule sales items.
By the way, I use an android app which has a plugin with these codes (below) to show only scheduled sales items in the app.
/** * Gey All Scheduled Sale Products OR special_deal_products */ public function pgs_woo_api_scheduled_sale_products($no_of_items=10,$app_ver=''){ global $wpdb,$woocommerce; $error = array( "status" => "error" ); $productdata = array(); $qur = "SELECT posts.ID, posts.post_parent FROM $wpdb->posts posts INNER JOIN $wpdb->postmeta ON (posts.ID = $wpdb->postmeta.post_id) INNER JOIN $wpdb->postmeta AS mt1 ON (posts.ID = mt1.post_id) WHERE posts.post_status = 'publish' AND (mt1.meta_key = '_sale_price_dates_to' AND mt1.meta_value >= ".time().") GROUP BY posts.ID ORDER BY rand()"; $product_ids_raw = $wpdb->get_results( $qur ); $product_ids_on_sale = array(); $image = ''; foreach ( $product_ids_raw as $product_raw ) { if(!empty($product_raw->post_parent)) { $product_ids_on_sale[] = $product_raw->post_parent; } else { $product_ids_on_sale[] = $product_raw->ID; } } $product_ids_on_sale = array_unique($product_ids_on_sale); if ( !empty( $product_ids_on_sale ) ) { foreach($product_ids_on_sale as $val){ $product_id = $val; $from = get_post_meta($product_id,'_sale_price_dates_from',true); $now = new DateTime(); $future_date = new DateTime(date('Y-m-d').' 24:00:00'); $product = wc_get_product( $product_id ); $interval = $future_date->diff($now); $deal_life = array( 'hours' => $interval->format('%h'), 'minutes' => $interval->format('%i'), 'seconds' => $interval->format('%s') ); if($from <= time()){ $data = $this->pgs_woo_api_set_productdata($product,$this->is_currency_switcher_active,$app_ver); $per = $this->pgs_woo_api_get_max_discount_percentage($product,$data); $data['deal_life'] = $deal_life; $data['percentage'] = $per; $productdata[] = $data; } } if($app_ver == ''){ $sts = array( "status" => "success", "products" => $productdata ); return $sts; } else { return $productdata; } } else { if($app_ver == ''){ $error['status'] = "error"; $error['message'] = esc_html__("No product found","pgs-woo-api"); return $error; } else { return $productdata; } } }
It’s not my code, but just a quick look at the original code that @yazdaniwp gave you makes me wonder if the problem might be with the operator in this section:
array( 'key' => '_sale_price_dates_to', 'value' => current_time( 'timestamp', 1 ), 'compare' => '<', ),
I’d try changing it to greater than the current time to see if you get the expected results.
@linux4me2 … Tired not working.. still No products found
Now you have me curious. I’ll give this a try and see what I can come up with.
Give this a try:
add_shortcode( 'scheduled_sale_products', 'scheduled_sale_products_shortcode' ); function scheduled_sale_products_shortcode($atts) { extract( shortcode_atts(array('limit' => '', 'columns'=>'' ), $atts)); $columns = ! empty($atts['columns'] ) ? $atts['columns'] : 3; //echo 'Products on sale: ' . implode( ',', wc_get_product_ids_on_sale() ) . '<br>'; //echo 'From time = ' . current_time('timestamp', 1) . '<br>'; $args = array( 'post_type'=>'product', 'post_status' =>'publish', 'post_in'=>implode( ',', wc_get_product_ids_on_sale() ), 'posts_per_page'=>$atts['limit'], 'meta_query'=>array( 'relation'=>'AND', array( 'key' =>'_sale_price_dates_from', 'value' =>current_time( 'timestamp', 1 ), 'compare'=>'>', ), array( 'key'=>'_sale_price_dates_to', 'value'=>current_time( 'timestamp', 1 ), '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(); }
The shortcode will now be:
[scheduled_sale_products limit=12 columns=3]
There were a couple of problems with the original code. One was a typo in
'post__in'
which had an extra underscore in the original code and was causing a MySQL error, and the other was with the handling of the operators in the times. I also removed some code that wasn’t used. It’s working for me to display a simple product that is scheduled for being on sale in the future. That code doesn’t work on variable products for me. I also added a parameter to the query to display only published products. On my test site using Twenty Sixteen, the layout is obviously going to need to be tweaked, but hey, it works now. ??I don’t understand how this is going to help you, because it doesn’t show the date the product is coming on sale, but maybe that’s what you want?
- This reply was modified 4 years, 8 months ago by linux4me2.
@linux4me2 yes its working now thanks.
Actually I need to show only currently running schedule sales products. For example, I set a product schedule date 01/03/2020 to 04/04/2020, i want to show these products. not upcoming schedule sales items. If possible help me ??
The key to which sale items are shown is in the two operators in the meta_Query:
array( 'key' =>'_sale_price_dates_from', 'value' =>current_time( 'timestamp', 1 ), 'compare'=>'>', ), array( 'key'=>'_sale_price_dates_to', 'value'=>current_time( 'timestamp', 1 ), 'compare'=>'>', ),
In the above code, it’s showing products with a “sale_price_dates_from” time that is greater than the current time, so it will only show future sales.
If you want to show products that are currently on sale and haven’t had their “sale_price_dates_to” time come around yet, you would change the operator like this:
array( 'key' =>'_sale_price_dates_from', 'value' =>current_time( 'timestamp', 1 ), 'compare'=>'<', ), array( 'key'=>'_sale_price_dates_to', 'value'=>current_time( 'timestamp', 1 ), 'compare'=>'>', ),
If you’re showing products that are currently on sale, why not just use the standard products short code or a block?
Hi @rifatspir
I’m apologize for being late.
This is your answer:
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' => 'product', '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(); }
Tested and works
Good luck
@linux4me2 Yes, it’s working now and perfectly showing the current single products schedule sales items. I need to customize the layout but I’m glad that its working as I wanted.
There is another code I found in stackoverflow but its working. So, I asked for help here.
However, standard products short code or a block doesn’t show up these running schedule items only. Its shows on sale items. I was looking for this badly, thanks for helping me.
If possible, please help me to show variable products also.
- This reply was modified 4 years, 8 months ago by rifatspir.
@yazdaniwp .. Thank you, it’s working now ??
Its shows only single products, not variable products. If possible, please help me to show variable products also.
With variable products too, it would just use an array for the post_type:
add_shortcode( 'scheduled_sale_products', 'scheduled_sale_products_shortcode' ); function scheduled_sale_products_shortcode($atts) { extract( shortcode_atts(array('limit' => '', 'columns'=>'' ), $atts)); $columns = ! empty($atts['columns'] ) ? $atts['columns'] : 3; //echo 'Products on sale: ' . implode( ',', wc_get_product_ids_on_sale() ) . '<br>'; //echo 'From time = ' . current_time('timestamp', 1) . '<br>'; $args = array( 'post_type'=>array('product', 'product_variation'), 'post_status' =>'publish', 'post_in'=>implode( ',', wc_get_product_ids_on_sale() ), 'posts_per_page'=>$atts['limit'], 'meta_query'=>array( 'relation'=>'AND', array( 'key' =>'_sale_price_dates_from', 'value' =>current_time( 'timestamp', 1 ), 'compare'=>'>', ), array( 'key'=>'_sale_price_dates_to', 'value'=>current_time( 'timestamp', 1 ), '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(); }
- The topic ‘Shortcode of scheduled sales products’ is closed to new replies.