woocommerce.php
2.6 KB
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
71
72
73
74
75
<?php
class CACIE_WooCommerce {
private function __construct() {}
/**
* Get the highest hierarchy role for a user
*
* @param int $userid ID of the user to get the role for
* @return bool|string User role name or false if the user doesn't exist
*/
static public function update_product_pricing( $product, $args ) {
$product = get_product( $product );
if ( ! $product ) {
return;
}
if ( $product->is_type( 'variable', 'grouped' ) ) {
return;
}
$args = wp_parse_args( $args, array(
'regular_price' => $product->get_regular_price(),
'sale_price' => $product->get_sale_price(),
'sale_price_dates_from' => $product->sale_price_dates_from,
'sale_price_dates_to' => $product->sale_price_dates_to
) );
$regular_price = ( $args['regular_price'] === '' ) ? '' : wc_format_decimal( $args['regular_price'] );
$sale_price = ( $args['sale_price'] === '' ) ? '' : wc_format_decimal( $args['sale_price'] );
update_post_meta( $product->id, '_regular_price', $regular_price );
update_post_meta( $product->id, '_sale_price', $sale_price );
$date_from = $args['sale_price_dates_from'] ? $args['sale_price_dates_from'] : '';
$date_to = $args['sale_price_dates_to'] ? $args['sale_price_dates_to'] : '';
// Dates
if ( $date_from ) {
update_post_meta( $product->id, '_sale_price_dates_from', strtotime( $date_from ) );
}
else {
update_post_meta( $product->id, '_sale_price_dates_from', '' );
}
if ( $date_to ) {
update_post_meta( $product->id, '_sale_price_dates_to', strtotime( $date_to ) );
}
else {
update_post_meta( $product->id, '_sale_price_dates_to', '' );
}
if ( $date_to && ! $date_from ) {
update_post_meta( $product->id, '_sale_price_dates_from', strtotime( 'NOW', current_time( 'timestamp' ) ) );
}
// Update price if on sale
if ( $sale_price !== '' && $date_to == '' && $date_from == '' ) {
update_post_meta( $product->id, '_price', wc_format_decimal( $sale_price ) );
}
else {
update_post_meta( $product->id, '_price', ( $regular_price === '' ) ? '' : wc_format_decimal( $regular_price ) );
}
if ( $sale_price !== '' && $date_from && strtotime( $date_from ) < strtotime( 'NOW', current_time( 'timestamp' ) ) )
update_post_meta( $product->id, '_price', wc_format_decimal( $sale_price ) );
if ( $date_to && strtotime( $date_to ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
update_post_meta( $product->id, '_price', ( $regular_price === '' ) ? '' : wc_format_decimal( $regular_price ) );
update_post_meta( $product->id, '_sale_price_dates_from', '' );
update_post_meta( $product->id, '_sale_price_dates_to', '' );
}
}
}