Merge pull request #2247 from GeertDD/DRY_sale
Created the `woocommerce_get_product_ids_on_sale()` function
This commit is contained in:
commit
c43fa99ca0
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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 ) );
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue