Removed cron job for sale prices

This commit is contained in:
Mike Jolley 2011-09-03 23:37:16 +01:00
parent 19e800e751
commit 0506c2e73c
3 changed files with 82 additions and 84 deletions

View File

@ -508,12 +508,25 @@ function woocommerce_process_product_meta( $post_id, $post ) {
// Update parent if grouped so price sorting works
if ($post->post_parent || $product_type=='grouped') :
if ($post->post_parent) :
$parent_id = $post->post_parent;
$post_parent = $post->post_parent;
else :
$parent_id = $post_id;
$post_parent = $post_id;
endif;
$children_by_price = get_posts( array(
'post_parent' => $post_parent,
'orderby' => 'meta_value_num',
'order' => 'asc',
'meta_key' => 'price',
'posts_per_page' => 1,
'post_type' => 'product'
));
if ($children_by_price) :
$children_by_price = $children_by_price[0];
$child = $children_by_price->ID;
update_post_meta( $post_parent, 'price', get_post_meta($child, 'price', true) );
endif;
woocommerce_grouped_price_sync( $parent_id );
endif;
// Stock Data

View File

@ -32,7 +32,8 @@ class woocommerce_product {
var $crosssell_ids;
var $product_type;
var $total_stock;
var $sale_price_dates_from;
var $sale_price_dates_to;
/**
* Loads all product data from custom fields
@ -41,7 +42,7 @@ class woocommerce_product {
*/
function woocommerce_product( $id ) {
$this->id = $id;
$this->id = (int) $id;
$product_custom_fields = get_post_custom( $this->id );
@ -714,5 +715,68 @@ class woocommerce_product {
return $available_attributes;
}
/**
* Checks sale data to see if the product is due to go on sale/sale has expired, and updates the main price
*/
function check_sale_price() {
if ($this->sale_price_dates_from && $this->sale_price_dates_from < strtotime('NOW')) :
if ($this->sale_price && $this->price!==$this->sale_price) :
$this->price = $this->sale_price;
update_post_meta($this->id, 'price', $this->price);
// Grouped products are affected by children
$this->grouped_product_price_sync();
endif;
endif;
if ($this->sale_price_dates_to && $this->sale_price_dates_to < strtotime('NOW')) :
if ($this->regular_price && $this->price!==$this->regular_price) :
$this->price = $this->regular_price;
update_post_meta($this->id, 'price', $this->price);
// Sale has expired - clear the schedule boxes
update_post_meta($this->id, 'sale_price_dates_from', '');
update_post_meta($this->id, 'sale_price_dates_to', '');
// Grouped products are affected by children
$this->grouped_product_price_sync();
endif;
endif;
}
/**
* Sync grouped products with the childs lowest price (so they can be sorted by price accurately)
**/
function grouped_product_price_sync() {
global $wpdb;
$post_parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = $this->id;");
if (!$post_parent) return;
$children_by_price = get_posts( array(
'post_parent' => $post_parent,
'orderby' => 'meta_value_num',
'order' => 'asc',
'meta_key' => 'price',
'posts_per_page' => 1,
'post_type' => 'product'
));
if ($children_by_price) :
$children_by_price = $children_by_price[0];
$child = $children_by_price->ID;
update_post_meta( $post_parent, 'price', get_post_meta($child, 'price', true) );
endif;
}
}

View File

@ -541,85 +541,6 @@ function woocommerce_exclude_order_comments( $clauses ) {
if (!is_admin()) add_filter('comments_clauses', 'woocommerce_exclude_order_comments');
/**
* Sync lowest price so grouped products can be sorted by price
**/
function woocommerce_grouped_price_sync( $parent_id ) {
$children_by_price = get_posts( array(
'post_parent' => $parent_id,
'orderby' => 'meta_value_num',
'order' => 'asc',
'meta_key' => 'price',
'posts_per_page' => 1,
'post_type' => 'product'
));
if ($children_by_price) :
$children_by_price = $children_by_price[0];
$child = $children_by_price->ID;
$low_price = get_post_meta($child, 'price', true);
update_post_meta( $parent_id, 'price', $low_price );
endif;
}
/**
* Cron Job - Update price if on sale
**/
function woocommerce_update_sale_prices() {
global $wpdb;
// On Sale Products
$on_sale = $wpdb->get_col("
SELECT post_id FROM $wpdb->postmeta
WHERE meta_key = 'sale_price_dates_from'
AND meta_value < ".strtotime('NOW')."
");
if ($on_sale) foreach ($on_sale as $product) :
$thispost = get_post($product);
$sale_price = get_post_meta($product, 'sale_price', true);
$price = get_post_meta($product, 'price', true);
if ($sale_price && $price!==$sale_price) update_post_meta($product, 'price', $sale_price);
if ($thispost->post_parent>0) woocommerce_grouped_price_sync( $thispost->post_parent );
endforeach;
// Expired Sales
$sale_expired = $wpdb->get_col("
SELECT post_id FROM $wpdb->postmeta
WHERE meta_key = 'sale_price_dates_to'
AND meta_value < ".strtotime('NOW')."
");
if ($sale_expired) foreach ($sale_expired as $product) :
$thispost = get_post($product);
$regular_price = get_post_meta($product, 'regular_price', true);
$price = get_post_meta($product, 'price', true);
if ($regular_price && $price!==$regular_price) update_post_meta($product, 'price', $regular_price);
// Sale has expired - clear the schedule boxes
update_post_meta($product, 'sale_price_dates_from', '');
update_post_meta($product, 'sale_price_dates_to', '');
if ($thispost->post_parent>0) woocommerce_grouped_price_sync( $thispost->post_parent );
endforeach;
}
function woocommerce_update_sale_prices_schedule_check(){
wp_schedule_event(time(), 'daily', 'woocommerce_update_sale_prices_schedule_check');
update_option('woocommerce_update_sale_prices', 'yes');
}
if (get_option('woocommerce_update_sale_prices')!='yes') woocommerce_update_sale_prices_schedule_check();
add_action('woocommerce_update_sale_prices_schedule_check', 'woocommerce_update_sale_prices');
/**
* Set up Roles & Capabilities
**/