wcpbc_scheduled_sales

Function which handles the start and end of scheduled sales via cron.

Description

wcpbc_scheduled_sales(); 

Usage

  1. if ( !function_exists( 'wcpbc_scheduled_sales' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-product-price-based-on-countries/includes/wcpbc-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wcpbc_scheduled_sales(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/wcpbc-functions.php  
  1. function wcpbc_scheduled_sales() {  
  2. global $wpdb; 
  3.  
  4. $region_keys = array_keys( WCPBC()->get_regions() );  
  5.  
  6. $sql = " 
  7. SELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta 
  8. LEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id 
  9. LEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id 
  10. WHERE postmeta.meta_key = %s 
  11. AND postmeta_2.meta_key in (%s, %s) 
  12. AND postmeta_3.meta_key in (%s, %s) 
  13. AND postmeta.meta_value > 0 
  14. AND postmeta.meta_value < %s 
  15. AND postmeta_2.meta_value != postmeta_3.meta_value 
  16. "; 
  17.  
  18. foreach ( $region_keys as $region_key ) { 
  19.  
  20. extract( wcpbc_get_product_meta_keys( $region_key ) ); 
  21.  
  22. $current_time = current_time( 'timestamp' ); 
  23.  
  24. // Sales which are due to start 
  25. $product_ids = $wpdb->get_col( $wpdb->prepare( $sql,  
  26. '_sale_price_dates_from',  
  27. $_price, $_variable_price,  
  28. $_sale_price, $_variable_sale_price,  
  29. $current_time  
  30. )); 
  31.  
  32. if ( $product_ids ) {  
  33. foreach ( $product_ids as $product_id ) {  
  34. if ( $sale_price = get_post_meta( $product_id, $meta_key_sale_price, true ) ) { 
  35. update_post_meta( $product_id, $meta_key_price, $sale_price ); 
  36.  
  37. delete_transient( 'wcpbc_products_onsale_' . $region_key ); 
  38.  
  39. // Sales which are due to end 
  40. $product_ids = $wpdb->get_col( $wpdb->prepare( $sql,  
  41. '_sale_price_dates_to',  
  42. $_price, $_variable_price,  
  43. $_sale_price, $_variable_sale_price,  
  44. $current_time  
  45. )); 
  46.  
  47. if ( $product_ids ) {  
  48. foreach ( $product_ids as $product_id ) {  
  49. $regular_price = get_post_meta( $product_id, $meta_key_regular_price, true ); 
  50. update_post_meta( $product_id, $meta_key_price, $regular_price ); 
  51. update_post_meta( $product_id, $meta_key_sale_price, '' ); 
  52.  
  53. delete_transient( 'wcpbc_products_onsale_' . $region_key ); 
  54. }