-
Notifications
You must be signed in to change notification settings - Fork 12
/
wc-ebs-cart-actions.php
129 lines (85 loc) · 4.14 KB
/
wc-ebs-cart-actions.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
class WC_EBS_Cart extends WC_Cart {
public function __construct() {
// get plugin options values
$this->options = get_option('wc_ebs_options');
add_filter('woocommerce_add_cart_item_data', array( $this, 'wc_ebs_add_cart_item_data'), 10, 2);
add_filter('woocommerce_get_cart_item_from_session', array( $this, 'wc_ebs_get_cart_item_from_session'), 10, 2);
add_filter('woocommerce_get_item_data', array( $this, 'wc_ebs_get_item_data'), 10, 2);
add_filter('woocommerce_add_cart_item', array( $this, 'wc_ebs_add_cart_item'), 10, 1);
}
function wc_ebs_add_cart_item_data($cart_item_meta, $product_id) {
global $woocommerce;
$booking_price = get_post_meta($product_id, '_new_price', true);
$base_price = get_post_meta($product_id, '_price', true);
$start = get_post_meta($product_id, '_start_date', true);
$end = get_post_meta($product_id, '_end_date', true);
$cart_item_meta['_new_price'] = $booking_price;
$cart_item_meta['_start_date'] = $start;
$cart_item_meta['_end_date'] = $end;
$this->wc_ebs_reset_product_meta( $product_id, $base_price, $start, $end );
return $cart_item_meta;
}
function wc_ebs_get_cart_item_from_session($cart_item, $values) {
// Add the form options meta to the cart item in case you want to do special stuff on the check out page.
if (isset($values['_new_price'])) {
$cart_item['_new_price'] = $values['_new_price'];
}
if (isset($values['_start_date'])) {
$cart_item['_start_date'] = $values['_start_date'];
}
if (isset($values['_end_date'])) {
$cart_item['_end_date'] = $values['_end_date'];
}
$this->wc_ebs_add_cart_item($cart_item);
return $cart_item;
}
// Reset meta data after adding to cart
function wc_ebs_reset_product_meta( $product_id, $base_price, $start, $end ) {
if ( get_post_meta( $product_id, '_new_price', true ) ) {
update_post_meta($product_id, '_new_price', $base_price);
}
if ( get_post_meta( $product_id, '_start_date', true ) ) {
delete_post_meta($product_id, '_start_date', $start);
}
if ( get_post_meta( $product_id, '_end_date', true ) ) {
delete_post_meta($product_id, '_end_date', $end);
}
}
function wc_ebs_get_item_data($other_data, $cart_item) {
if ( isset($cart_item['_start_date']) && $cart_item['_start_date'] ) {
$startDate = $cart_item['_start_date'];
// Add custom data to product data
$other_data[] = array('name' => __( $this->options['wc_ebs_start_date_text'], 'wc_ebs' ), 'value' => $startDate);
}
if ( isset($cart_item['_end_date']) && $cart_item['_end_date'] ) {
$endDate = $cart_item['_end_date'];
// Add custom data to product data
$other_data[] = array('name' => __( $this->options['wc_ebs_end_date_text'], 'wc_ebs' ), 'value' => $endDate);
}
return $other_data;
}
function wc_ebs_add_cart_item($cart_item) {
global $woocommerce;
if ( isset($cart_item['_new_price']) && $cart_item['_new_price'] > 0 ) {
$booking_price = $cart_item['_new_price'];
$cart_item['data']->set_price($booking_price);
}
return $cart_item;
}
}
new WC_EBS_Cart();
class WC_EBS_Checkout extends WC_Checkout {
public function __construct() {
// get plugin options values
$this->options = get_option('wc_ebs_options');
add_action('woocommerce_add_order_item_meta', array($this, 'wc_ebs_add_order_meta' ), 10, 2);
}
public function wc_ebs_add_order_meta($item_id, $values) {
if ( ! empty( $values['_start_date'] ) )
woocommerce_add_order_item_meta( $item_id, $this->options['wc_ebs_start_date_text'], $values['_start_date'] );
if ( ! empty( $values['_end_date'] ) )
woocommerce_add_order_item_meta( $item_id, $this->options['wc_ebs_end_date_text'], $values['_end_date'] );
}
}
new WC_EBS_Checkout();