forked from jigoshop/jigoshop
/
jigoshop_cron.php
70 lines (53 loc) · 2.08 KB
/
jigoshop_cron.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Cron Task
*
* DISCLAIMER
*
* Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
* versions in the future. If you wish to customise Jigoshop core for your needs,
* please use our GitHub repository to publish essential changes for consideration.
*
* @package Jigoshop
* @category Core
* @author Jigowatt
* @copyright Copyright (c) 2011 Jigowatt Ltd.
* @license http://jigoshop.com/license/commercial-edition
*/
/**
* Update price if on sale
*/
function jigoshop_update_sale_prices() {
global $wpdb;
// On Sale Products
$on_sale = $wpdb->get_results("
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) :
$data = unserialize( get_post_meta($product, 'product_data', true) );
$price = get_post_meta($product, 'price', true);
if ($data['sale_price'] && $price!==$data['sale_price']) update_post_meta($product, 'price', $data['sale_price']);
endforeach;
// Expired Sales
$sale_expired = $wpdb->get_results("
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) :
$data = unserialize( get_post_meta($product, 'product_data', true) );
$price = get_post_meta($product, 'price', true);
if ($data['regular_price'] && $price!==$data['regular_price']) update_post_meta($product, 'price', $data['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', '');
endforeach;
}
function jigoshop_update_sale_prices_schedule_check(){
wp_schedule_event(time(), 'daily', 'jigoshop_update_sale_prices_schedule_check');
update_option('jigoshop_update_sale_prices', 'yes');
}
if (get_option('jigoshop_update_sale_prices')!='yes') jigoshop_update_sale_prices_schedule_check();
add_action('jigoshop_update_sale_prices_schedule_check', 'jigoshop_update_sale_prices');