PluginProbe
MailerLite – WooCommerce integration / 2.0.4
MailerLite – WooCommerce integration v2.0.4
3.1.27 3.1.25 3.1.26 3.1.24 3.1.23 3.1.22 3.1.21 3.1.20 3.1.19 3.1.18 3.1.17 3.1.16 3.1.15 1.8.7 1.8.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 All 148 releases
woo-mailerlite / includes / hooks.php

hooks.php in MailerLite – WooCommerce integration 2.0.4, at includes/hooks.php

327 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use MailerLite\Includes\Classes\Process\CartProcess;
4 use MailerLite\Includes\Classes\Process\OrderProcess;
5 use MailerLite\Includes\Classes\Process\OrderSyncProcess;
6 use MailerLite\Includes\Classes\Process\ProductProcess;
7 use MailerLite\Includes\Classes\Settings\MailerLiteSettings;
8 use MailerLite\Includes\Classes\Settings\SynchronizeSettings;
9
10
11 /**
12 * Shows the final purchase total at the bottom of the checkout page
13 *
14 * @return void
15 * @since 1.5
16 */
17 function woo_ml_checkout_label()
18 {
19 if ( ! MailerLiteSettings::getInstance()->isActive()) {
20 return;
21 }
22
23 $checkout = MailerLiteSettings::getInstance()->getMlOption('checkout', 'no');
24
25 if ('yes' != $checkout) {
26 return;
27 }
28
29 $group = MailerLiteSettings::getInstance()->getMlOption('group');
30
31 if (empty($group)) {
32 return;
33 }
34
35 $label = MailerLiteSettings::getInstance()->getMlOption('checkout_label');
36 $preselect = MailerLiteSettings::getInstance()->getMlOption('checkout_preselect', 'no');
37 $hidden = MailerLiteSettings::getInstance()->getMlOption('checkout_hide', 'no');
38
39 if ('yes' === $hidden) {
40 ?>
41 <input name="woo_ml_subscribe" type="hidden" id="woo_ml_subscribe" value="1" checked="checked"/>
42 <?php
43 } else {
44
45 woocommerce_form_field('woo_ml_subscribe', array(
46 'type' => 'checkbox',
47 'label' => __($label, 'woo-mailerlite'),
48 ), (bool) ($_COOKIE['mailerlite_accepts_marketing'] ?? 'yes' === $preselect));
49 }
50 }
51
52 $checkout_position = MailerLiteSettings::getInstance()->getMlOption('checkout_position', 'checkout_billing');
53 $checkout_position_hook = 'woocommerce_' . $checkout_position;
54
55 if ($checkout_position !== 'checkout_billing_email') {
56 add_action($checkout_position_hook, 'woo_ml_checkout_label', 20);
57 }
58
59 /**
60 * Remove (optional) string for ML label
61 *
62 */
63 function remove_ml_checkout_optional_text($field, $key, $args, $value)
64 {
65
66 if (is_checkout() && ! is_wc_endpoint_url() && strpos($field,
67 'woo_ml_subscribe') !== false && get_option('ml_account_authenticated')) {
68
69 $optional = '&nbsp;<span class="optional">(' . esc_html__('optional', 'woocommerce') . ')</span>';
70 $field = str_replace($optional, '', $field);
71 }
72
73 return $field;
74 }
75
76 add_filter('woocommerce_form_field', 'remove_ml_checkout_optional_text', 10, 4);
77
78 /**
79 * Maybe prepare signup
80 *
81 * @param $order_id
82 */
83 function woo_ml_checkout_maybe_prepare_signup($order_id)
84 {
85
86 if (isset($_POST['woo_ml_subscribe']) && '1' == $_POST['woo_ml_subscribe']) {
87 OrderProcess::getInstance()->setOrderCustomerSubscribe($order_id);
88 }
89 }
90
91 add_action('woocommerce_checkout_update_order_meta', 'woo_ml_checkout_maybe_prepare_signup');
92
93 /**
94 * Process order completed (and finally paid)
95 *
96 * @param $order_id
97 */
98 function woo_ml_process_order_completed($order_id)
99 {
100
101 if ( ! woo_ml_integration_setup_completed()) {
102 woo_ml_setup_integration();
103 }
104
105 if ( ! woo_ml_old_integration()) {
106 OrderProcess::getInstance()->sendCompletedOrder($order_id);
107 }
108
109 OrderProcess::getInstance()->processOrderTracking($order_id);
110 }
111
112 add_action('woocommerce_order_status_completed', 'woo_ml_process_order_completed');
113
114 function woo_ml_proceed_to_checkout()
115 {
116
117 if ( ! get_option('ml_account_authenticated')) {
118
119 return false;
120 }
121
122 if ( ! woo_ml_old_integration()) {
123 CartProcess::getInstance()->sendCart();
124 }
125 }
126
127 add_action('woocommerce_add_to_cart', 'woo_ml_proceed_to_checkout');
128 add_action('woocommerce_cart_item_removed', 'woo_ml_proceed_to_checkout');
129
130 function woo_ml_cart_action_updated($cart_updated)
131 {
132
133 woo_ml_proceed_to_checkout();
134
135 return $cart_updated;
136 }
137
138 add_filter('woocommerce_update_cart_action_cart_updated', 'woo_ml_cart_action_updated');
139
140 function woo_ml_order_status_change($order_id)
141 {
142 if ( ! woo_ml_old_integration()) {
143 OrderProcess::getInstance()->paymentStatusProcessing($order_id);
144 }
145 }
146
147 add_action('woocommerce_order_status_changed', 'woo_ml_order_status_change');
148
149
150 function woo_ml_enqueue_styles()
151 {
152
153 if ( ! get_option('ml_account_authenticated')) {
154
155 return false;
156 }
157
158 wp_enqueue_style('related-styles', plugins_url('/../public/css/style.css', __FILE__));
159 }
160
161 add_action('wp_enqueue_scripts', 'woo_ml_enqueue_styles');
162
163 /**
164 * WooCommerce product update
165 *
166 * @param $product_id
167 */
168 function woo_ml_product_update($product_id)
169 {
170
171 ProductProcess::getInstance()->wpSyncProduct($product_id);
172 }
173
174 add_action('woocommerce_update_product', 'woo_ml_product_update', 10, 1);
175
176 /**
177 * WooCommerce order update
178 *
179 * @param $order_id
180 */
181 function woo_ml_order_update($order_id, $order)
182 {
183
184 OrderSyncProcess::getInstance()->syncOrder($order_id, $order);
185 }
186
187 add_action('woocommerce_saved_order_items', 'woo_ml_order_update', 10, 2);
188
189 /**
190 * WooCommerce bulk order update
191 *
192 * @param $redirect_to
193 * @param $action
194 * @param $order_ids
195 */
196 function woo_ml_bulk_order_update($redirect_to, $action, $order_ids)
197 {
198
199 OrderSyncProcess::getInstance()->syncBulkOrder($action, $order_ids);
200
201 return $redirect_to;
202 }
203
204 add_filter('handle_bulk_actions-edit-shop_order', 'woo_ml_bulk_order_update', 10, 3);
205
206 /**
207 * WooCommerce order cancel
208 *
209 * @param $order_id
210 */
211 function woo_ml_order_cancel($order_id)
212 {
213
214 OrderProcess::getInstance()->cancelOrder($order_id);
215 }
216
217 add_action( 'woocommerce_order_status_cancelled', 'woo_ml_order_cancel', 21, 1 );
218
219 /**
220 * WP Post delete
221 *
222 * @param $post_id
223 */
224 function woo_ml_post_delete($post_id)
225 {
226
227 SynchronizeSettings::getInstance()->syncPostDelete($post_id);
228 }
229
230 add_action('delete_post', 'woo_ml_post_delete', 10);
231
232 function woo_ml_customer_update($customer_id, $customer_data = '')
233 {
234
235 mailerlite_wp_sync_ecommerce_customer($customer_id);
236 }
237
238 add_action('woocommerce_update_customer', 'woo_ml_customer_update', 99, 2);
239 add_action('user_register', 'woo_ml_customer_update', 10, 1);
240 add_action('profile_update', 'woo_ml_customer_update', 10, 2);
241
242 /**
243 * WP Product Category create/update
244 *
245 * @param $category_id
246 */
247 function woo_ml_product_category_sync($category_id)
248 {
249
250 ProductProcess::getInstance()->syncProductCategory($category_id);
251 }
252
253 add_action('created_product_cat', 'woo_ml_product_category_sync', 10, 2);
254 add_action('edited_product_cat', 'woo_ml_product_category_sync', 10, 2);
255
256 function woo_ml_product_category_delete($term, $tt_id, $deleted_term, $object_ids)
257 {
258
259 // Avoid auto save from calling API
260 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
261 return;
262 }
263
264 ProductProcess::getInstance()->deleteProductCategory($term);
265 }
266
267 add_action('delete_product_cat', 'woo_ml_product_category_delete', 10, 4);
268
269 function woo_ml_billing_checkout_fields($fields) {
270
271 if ( ! MailerLiteSettings::getInstance()->isActive()) {
272 return $fields;
273 }
274
275 $checkout = MailerLiteSettings::getInstance()->getMlOption('checkout', 'no');
276
277 if ('yes' != $checkout) {
278 return $fields;
279 }
280
281 $group = MailerLiteSettings::getInstance()->getMlOption('group');
282
283 if (empty($group)) {
284 return $fields;
285 }
286
287 $checkout_position = MailerLiteSettings::getInstance()->getMlOption('checkout_position', 'checkout_billing');
288
289 if ($checkout_position !== 'checkout_billing_email') {
290 return $fields;
291 }
292
293 $label = MailerLiteSettings::getInstance()->getMlOption('checkout_label');
294 $preselect = MailerLiteSettings::getInstance()->getMlOption('checkout_preselect', 'no');
295 $hidden = MailerLiteSettings::getInstance()->getMlOption('checkout_hide', 'no');
296
297 $new_billing_fields = [];
298
299 foreach ($fields['billing'] as $key => $field) {
300 $new_billing_fields[$key] = $field;
301
302 // Add the custom checkbox field right after 'billing_email'.
303 if ($key === 'billing_email') {
304
305 $new_billing_fields['woo_ml_subscribe'] = [
306 'type' => ($hidden === 'no') ? 'checkbox' : 'hidden',
307 'default' => (bool) ($_COOKIE['mailerlite_accepts_marketing'] ?? 'yes' === $preselect),
308 'required' => false,
309 ];
310
311 if ($hidden === 'no') {
312 $new_billing_fields['woo_ml_subscribe']['label'] = __($label, 'woo-mailerlite');
313 } else {
314 $new_billing_fields['woo_ml_subscribe']['custom_attributes'] = [
315 'checked' => 'checked',
316 ];
317 }
318 }
319 }
320
321 // Replace the 'billing' fields with the modified field order.
322 $fields['billing'] = $new_billing_fields;
323
324 return $fields;
325 }
326
327 add_filter('woocommerce_checkout_fields', 'woo_ml_billing_checkout_fields', PHP_INT_MAX);