From e806d4c1938b1a1a8795c9f027011414685eb6db Mon Sep 17 00:00:00 2001 From: Geert De Deckere Date: Wed, 16 Jan 2013 20:04:01 +0100 Subject: [PATCH] Created the `woocommerce_get_product_ids_on_sale()` function --- classes/class-wc-shortcodes.php | 34 +---------------- classes/widgets/class-wc-widget-onsale.php | 35 +---------------- woocommerce-core-functions.php | 44 ++++++++++++++++++++++ 3 files changed, 46 insertions(+), 67 deletions(-) diff --git a/classes/class-wc-shortcodes.php b/classes/class-wc-shortcodes.php index db4088cafed..8c975a4fcb1 100644 --- a/classes/class-wc-shortcodes.php +++ b/classes/class-wc-shortcodes.php @@ -624,39 +624,7 @@ class WC_Shortcodes { ), $atts ) ); // Get products on sale - if ( false === ( $product_ids_on_sale = get_transient( 'wc_products_onsale' ) ) ) { - - $meta_query = array(); - - $meta_query[] = array( - 'key' => '_sale_price', - 'value' => 0, - 'compare' => '>', - 'type' => 'NUMERIC' - ); - - $on_sale = get_posts(array( - 'post_type' => array('product', 'product_variation'), - 'posts_per_page' => -1, - 'post_status' => 'publish', - 'meta_query' => $meta_query, - 'fields' => 'id=>parent' - )); - - $product_ids = array_keys( $on_sale ); - $parent_ids = array_values( $on_sale ); - - // Check for scheduled sales which have not started - foreach ( $product_ids as $key => $id ) - if ( get_post_meta( $id, '_sale_price_dates_from', true ) > current_time('timestamp') ) - unset( $product_ids[ $key ] ); - - $product_ids_on_sale = array_unique( array_merge( $product_ids, $parent_ids ) ); - - set_transient( 'wc_products_onsale', $product_ids_on_sale ); - } - - $product_ids_on_sale[] = 0; + $product_ids_on_sale = woocommerce_get_product_ids_on_sale(); $meta_query = array(); $meta_query[] = $woocommerce->query->visibility_meta_query(); diff --git a/classes/widgets/class-wc-widget-onsale.php b/classes/widgets/class-wc-widget-onsale.php index bdd8f6e2dcc..b334537df10 100644 --- a/classes/widgets/class-wc-widget-onsale.php +++ b/classes/widgets/class-wc-widget-onsale.php @@ -76,40 +76,7 @@ class WC_Widget_Onsale extends WP_Widget { $number = 15; // Get products on sale - if ( false === ( $product_ids_on_sale = get_transient( 'wc_products_onsale' ) ) ) { - - $meta_query = array(); - - $meta_query[] = array( - 'key' => '_sale_price', - 'value' => 0, - 'compare' => '>', - 'type' => 'NUMERIC' - ); - - $on_sale = get_posts(array( - 'post_type' => array('product', 'product_variation'), - 'posts_per_page' => -1, - 'post_status' => 'publish', - 'meta_query' => $meta_query, - 'fields' => 'id=>parent' - )); - - $product_ids = array_keys( $on_sale ); - $parent_ids = array_values( $on_sale ); - - // Check for scheduled sales which have not started - foreach ( $product_ids as $key => $id ) - if ( get_post_meta( $id, '_sale_price_dates_from', true ) > current_time('timestamp') ) - unset( $product_ids[ $key ] ); - - $product_ids_on_sale = array_unique( array_merge( $product_ids, $parent_ids ) ); - - set_transient( 'wc_products_onsale', $product_ids_on_sale ); - - } - - $product_ids_on_sale[] = 0; + $product_ids_on_sale = woocommerce_get_product_ids_on_sale(); $meta_query = array(); $meta_query[] = $woocommerce->query->visibility_meta_query(); diff --git a/woocommerce-core-functions.php b/woocommerce-core-functions.php index 3330ebbd0d8..21175ac1615 100644 --- a/woocommerce-core-functions.php +++ b/woocommerce-core-functions.php @@ -32,6 +32,50 @@ function get_product( $the_product = false, $args = array() ) { return $woocommerce->product_factory->get_product( $the_product, $args ); } +/** + * Function that returns an array containing the IDs of the products that are on sale. + * + * @access public + * @return array + */ +function woocommerce_get_product_ids_on_sale() { + // Load from cache + $product_ids_on_sale = get_transient( 'wc_products_onsale' ); + + // Valid cache found + if ( false !== $product_ids_on_sale ) + return $product_ids_on_sale; + + $on_sale = get_posts( array( + 'post_type' => array( 'product', 'product_variation' ), + 'posts_per_page' => -1, + 'post_status' => 'publish', + 'meta_query' => array( array( + 'key' => '_sale_price', + 'value' => 0, + 'compare' => '>', + 'type' => 'NUMERIC', + ) ), + 'fields' => 'id=>parent', + ) ); + + $product_ids = array_keys( $on_sale ); + $parent_ids = array_values( $on_sale ); + + // Check for scheduled sales which have not started + foreach ( $product_ids as $key => $id ) { + if ( get_post_meta( $id, '_sale_price_dates_from', true ) > current_time( 'timestamp' ) ) { + unset( $product_ids[ $key ] ); + } + } + + $product_ids_on_sale = array_unique( array_merge( $product_ids, $parent_ids ) ); + + set_transient( 'wc_products_onsale', $product_ids_on_sale ); + + return $product_ids_on_sale; +} + function woocommerce_sanitize_taxonomy_name( $taxonomy ) { return str_replace( array( ' ', '_' ), '-', strtolower( $taxonomy ) ); }