1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Handle WooCommerce Integration: add a checkbox for subscribers on WooCommerce's checkout page. |
4
|
|
|
* |
5
|
|
|
* @since 6.0.0 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
// Prevent direct access to the file. |
9
|
|
|
defined( 'ABSPATH' ) || die( esc_html_e( "Whoops, you shouldn't be accessing this file directly. Abort!", 'yikes-inc-easy-mailchimp-extender' ) ); |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* WooCo Checkbox Integration. |
13
|
|
|
*/ |
14
|
|
|
class Yikes_Easy_MC_WooCommerce_Checkbox_Class extends Yikes_Easy_MC_Checkbox_Integration_Class { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The integration type. |
18
|
|
|
* |
19
|
|
|
* @var string $type |
20
|
|
|
*/ |
21
|
|
|
protected $type = 'woocommerce_checkout_form'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor. |
25
|
|
|
*/ |
26
|
|
|
public function __construct() { |
27
|
|
|
add_action( 'init', array( $this, 'determine_checkbox_placement' ), 1000 ); |
28
|
|
|
add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'save_woocommerce_checkout_checkbox_value' ) ); |
29
|
|
|
add_action( 'woocommerce_checkout_order_processed', array( $this, 'subscribe_from_woocommerce_checkout' ) ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Filter where the WooCo checkbox will go. |
34
|
|
|
*/ |
35
|
|
|
public function determine_checkbox_placement() { |
36
|
|
|
|
37
|
|
|
$default_checkbox_placement = apply_filters( 'yikes-mailchimp-wooco-integration-checkbox-checkout-fields', true ); |
38
|
|
|
|
39
|
|
|
if ( $default_checkbox_placement ) { |
40
|
|
|
add_filter( 'woocommerce_checkout_fields', array( $this, 'add_checkout_field' ), 20 ); |
41
|
|
|
} else { |
42
|
|
|
$checkbox_location = apply_filters( 'yikes_mailchimp_wooco_integration_placement_filter', 'woocommerce_review_order_before_submit' ); |
43
|
|
|
add_action( $checkbox_location, array( $this, 'output_checkbox' ) ); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Print the checkbox to the page. |
49
|
|
|
*/ |
50
|
|
|
public function output_checkbox() { |
51
|
|
|
if ( $this->is_user_already_subscribed( $this->type ) ) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
echo $this->yikes_get_checkbox(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add the checkbox to WooCommerce's checkout fields array. |
59
|
|
|
* |
60
|
|
|
* @param array $fields WooCommerce's array of checkout fields. |
61
|
|
|
* @return array $fields WooCommerce's array of checkout fields with our checkbox appended. |
62
|
|
|
*/ |
63
|
|
|
public function add_checkout_field( $fields ) { |
64
|
|
|
|
65
|
|
|
// Get checkbox data. |
66
|
|
|
$checkbox_options = get_option( 'optin-checkbox-init', array() ); |
67
|
|
|
|
68
|
|
|
// Only display the field if a list is set. |
69
|
|
|
if ( isset( $checkbox_options[ $this->type ] ) && isset( $checkbox_options[ $this->type ]['associated-list'] ) && '-' !== $checkbox_options[ $this->type ]['associated-list'] ) { |
70
|
|
|
|
71
|
|
|
if ( $this->is_user_already_subscribed( $this->type ) ) { |
72
|
|
|
return $fields; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$precheck = isset( $checkbox_options[ $this->type ]['precheck'] ) && 'true' === $checkbox_options[ $this->type ]['precheck'] ? '1' : '0'; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Filter where the checkbox goes. |
79
|
|
|
* |
80
|
|
|
* See this WooCo article for possible values: https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ |
81
|
|
|
* |
82
|
|
|
* @param string | Which set of fields the checkbox should go into |
83
|
|
|
*/ |
84
|
|
|
$field_placement = apply_filters( 'yikes-mailchimp-wooco-integration-checkbox-placement', 'billing' ); |
85
|
|
|
|
86
|
|
|
$yikes_checkbox = array( |
87
|
|
|
'id' => 'yikes_mailchimp_checkbox_' . $this->type, |
88
|
|
|
'type' => 'checkbox', |
89
|
|
|
'class' => apply_filters( 'yikes-mailchimp-wooco-integration-checkbox-classes', array( 'form-row-wide' ) ), |
90
|
|
|
'label' => $checkbox_options[ $this->type ]['label'], |
91
|
|
|
'default' => $precheck, |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Filter the checkbox data. |
96
|
|
|
* |
97
|
|
|
* See this WooCo article for possible values: https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ |
98
|
|
|
* |
99
|
|
|
* @param array $yikes_checkbox The checkbox's fields. |
100
|
|
|
* @return array $yikes_checkbox The checkbox's fields. |
101
|
|
|
*/ |
102
|
|
|
$yikes_checkbox = apply_filters( 'yikes_mailchimp_wooco_integration_checkbox_field', $yikes_checkbox, $checkbox_options[ $this->type ] ); |
103
|
|
|
|
104
|
|
|
$fields[ $field_placement ][ 'yikes_mailchimp_checkbox_' . $this->type ] = $yikes_checkbox; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $fields; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Save the checkbox's status as post meta to the order. |
112
|
|
|
* |
113
|
|
|
* This allows us to run the subscription request after the order has been processed. |
114
|
|
|
* |
115
|
|
|
* @param int $order_id The WooCo order ID. |
116
|
|
|
*/ |
117
|
|
|
public function save_woocommerce_checkout_checkbox_value( $order_id ) { |
118
|
|
|
update_post_meta( $order_id, 'yikes_easy_mailchimp_optin', $this->was_checkbox_checked( $this->type ) ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Subscribe the user if they so chose. |
123
|
|
|
* |
124
|
|
|
* @param int $order_id The WooCo Order ID. |
125
|
|
|
*/ |
126
|
|
|
public function subscribe_from_woocommerce_checkout( $order_id ) { |
127
|
|
|
$do_optin = get_post_meta( $order_id, 'yikes_easy_mailchimp_optin', true ); |
128
|
|
|
|
129
|
|
|
if ( '1' === $do_optin ) { |
130
|
|
|
$order = new WC_Order( $order_id ); |
131
|
|
|
$email = $order->get_billing_email(); |
132
|
|
|
$merge_vars = array( |
133
|
|
|
'FNAME' => $order->get_billing_first_name(), |
134
|
|
|
'LNAME' => $order->get_billing_last_name(), |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$integration_vars = array( |
138
|
|
|
'order' => $order, |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
// Subscribe the user. |
142
|
|
|
$this->subscribe_user_integration( $email, $this->type, $merge_vars, $integration_vars ); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$yikes_easy_mc_woocommerce_checkbox_class = new Yikes_Easy_MC_WooCommerce_Checkbox_Class(); |
148
|
|
|
|