| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* WooCommerce Cart Abandonment Recovery |
| 8 |
* |
| 9 |
* @package Wa_Notifier |
| 10 |
*/ |
| 11 |
class Notifier_WCAR { |
| 12 |
|
| 13 |
/** |
| 14 |
* Init. |
| 15 |
*/ |
| 16 |
public static function init() { |
| 17 |
add_filter( 'notifier_notification_triggers', array( __CLASS__, 'add_notification_triggers'), 10 ); |
| 18 |
add_filter( 'notifier_notification_merge_tags', array( __CLASS__, 'wcar_merge_tags') ); |
| 19 |
add_filter( 'notifier_notification_recipient_fields', array( __CLASS__, 'wcar_recipient_fields') ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Add notification trigger |
| 24 |
*/ |
| 25 |
public static function add_notification_triggers ($triggers) { |
| 26 |
$merge_tag_types = array('WooCommerce', 'WooCommerce Cart Abandonment Recovery'); |
| 27 |
$recipient_types = array('WooCommerce Cart Abandonment Recovery'); |
| 28 |
$triggers['Cart Abandonment Recovery for WooCommerce'][] = array( |
| 29 |
'id' => 'woocommerce_cart_abandonment_recovery', |
| 30 |
'label' => 'Cart is abandoned (WCAR)', |
| 31 |
'description' => 'Trigger notification when cart is abandoned as per your settings in the plugin.', |
| 32 |
'merge_tags' => Notifier_Notification_Merge_Tags::get_merge_tags($merge_tag_types), |
| 33 |
'recipient_fields' => Notifier_Notification_Merge_Tags::get_recipient_fields($recipient_types), |
| 34 |
'action' => array( |
| 35 |
'hook' => 'wcf_ca_process_abandoned_order', |
| 36 |
'callback' => function ( $checkout_details ) { |
| 37 |
$checkout_details = (array) $checkout_details; |
| 38 |
Notifier_Notification_Triggers::send_trigger_request('woocommerce_cart_abandonment_recovery', $checkout_details); |
| 39 |
} |
| 40 |
) |
| 41 |
); |
| 42 |
return $triggers; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Add merge tags |
| 47 |
*/ |
| 48 |
public static function wcar_merge_tags($merge_tags) { |
| 49 |
$merge_tags['WooCommerce Cart Abandonment Recovery'] = array( |
| 50 |
array( |
| 51 |
'id' => 'wcar_cart_products_list', |
| 52 |
'label' => 'Abandoned cart products list', |
| 53 |
'preview_value' => '', |
| 54 |
'return_type' => 'text', |
| 55 |
'value' => function ($checkout_details) { |
| 56 |
$value = Cartflows_Ca_Helper::get_instance()->get_comma_separated_products( $checkout_details['cart_contents'] ); |
| 57 |
return (string) $value; |
| 58 |
} |
| 59 |
), |
| 60 |
array( |
| 61 |
'id' => 'wcar_cart_total', |
| 62 |
'label' => 'Abandoned cart total', |
| 63 |
'preview_value' => '', |
| 64 |
'return_type' => 'text', |
| 65 |
'value' => function ($checkout_details) { |
| 66 |
$value = isset($checkout_details['cart_total']) ? $checkout_details['cart_total'] : ''; |
| 67 |
return (string) $value; |
| 68 |
} |
| 69 |
), |
| 70 |
array( |
| 71 |
'id' => 'wcar_cart_datetime', |
| 72 |
'label' => 'Abandoned cart datetime', |
| 73 |
'preview_value' => '', |
| 74 |
'return_type' => 'text', |
| 75 |
'value' => function ($checkout_details) { |
| 76 |
$value = isset($checkout_details['time']) ? $checkout_details['time'] : ''; |
| 77 |
return (string) $value; |
| 78 |
} |
| 79 |
), |
| 80 |
array( |
| 81 |
'id' => 'wcar_cart_checkout_url', |
| 82 |
'label' => 'Abandoned cart checkout URL', |
| 83 |
'preview_value' => '', |
| 84 |
'return_type' => 'text', |
| 85 |
'value' => function ($checkout_details) { |
| 86 |
$token_data = array( |
| 87 |
'wcf_session_id' => $checkout_details['session_id'], |
| 88 |
'wcf_preview_email' => false, |
| 89 |
); |
| 90 |
$checkout_url = Cartflows_Ca_Helper::get_instance()->get_checkout_url( $checkout_details['checkout_id'], $token_data ); |
| 91 |
return (string) $checkout_url; |
| 92 |
} |
| 93 |
), |
| 94 |
array( |
| 95 |
'id' => 'wcar_cart_customer_email', |
| 96 |
'label' => 'Billing email', |
| 97 |
'preview_value' => '', |
| 98 |
'return_type' => 'text', |
| 99 |
'value' => function ($checkout_details) { |
| 100 |
$value = isset($checkout_details['email']) ? $checkout_details['email'] : ''; |
| 101 |
return (string) $value; |
| 102 |
} |
| 103 |
), |
| 104 |
); |
| 105 |
|
| 106 |
$wcar_other_fields = array ( |
| 107 |
'first_name' => array( |
| 108 |
'preview' => 'John', |
| 109 |
'label' => 'Billing first name' |
| 110 |
), |
| 111 |
'last_name' => array( |
| 112 |
'preview' => 'Doe', |
| 113 |
'label' => 'Billing last name' |
| 114 |
), |
| 115 |
'billing_company' => array('preview' => 'ABC Inc'), |
| 116 |
'billing_address_1' => array('preview' => '123, XYZ Street'), |
| 117 |
'billing_address_2' => array('preview' => 'XYZ Avenue'), |
| 118 |
'billing_city' => array( |
| 119 |
'preview' => 'New York', |
| 120 |
'value' => function ($other_data) { |
| 121 |
$location = isset($other_data['wcf_location']) ? explode(', ', $other_data['wcf_location']) : ''; |
| 122 |
if(!isset($location[1])){ |
| 123 |
return ''; |
| 124 |
} |
| 125 |
return (string) $location[1]; |
| 126 |
} |
| 127 |
), |
| 128 |
'billing_postcode' => array('preview' => '12345'), |
| 129 |
'billing_state' => array( |
| 130 |
'preview' => 'NY', |
| 131 |
'value' => function ($other_data) { |
| 132 |
$state_code = isset($other_data['wcf_billing_state']) ? $other_data['wcf_billing_state'] : ''; |
| 133 |
$location = isset($other_data['wcf_location']) ? explode(', ', $other_data['wcf_location']) : ''; |
| 134 |
$country_code = isset($location[0]) ? $location[0] : ''; |
| 135 |
if('' == $state_code || '' == $country_code){ |
| 136 |
return ''; |
| 137 |
} |
| 138 |
return WC()->countries->states[$country_code][$state_code]; |
| 139 |
} |
| 140 |
), |
| 141 |
'billing_country' => array( |
| 142 |
'preview' => 'US', |
| 143 |
'value' => function ($other_data) { |
| 144 |
$location = isset($other_data['wcf_location']) ? explode(', ', $other_data['wcf_location']) : ''; |
| 145 |
$country_code = isset($location[0]) ? $location[0] : ''; |
| 146 |
if('' == $country_code){ |
| 147 |
return ''; |
| 148 |
} |
| 149 |
return WC()->countries->countries[$country_code]; |
| 150 |
} |
| 151 |
), |
| 152 |
'phone_number' => array( |
| 153 |
'preview' => '987 654 321', |
| 154 |
'label' => 'Billing phone number' |
| 155 |
), |
| 156 |
'shipping_first_name' => array('preview' => 'John'), |
| 157 |
'shipping_last_name' => array('preview' => 'Doe'), |
| 158 |
'shipping_company' => array('preview' => 'ABC Inc'), |
| 159 |
'shipping_address_1' => array('preview' => '123, XYZ Street'), |
| 160 |
'shipping_address_2' => array('preview' => 'XYZ Avenue'), |
| 161 |
'shipping_city' => array('preview' => 'New York'), |
| 162 |
'shipping_postcode' => array('preview' => '12345'), |
| 163 |
'shipping_state' => array( |
| 164 |
'preview' => 'NY', |
| 165 |
'value' => function ($other_data) { |
| 166 |
$state_code = isset($other_data['wcf_shipping_state']) ? $other_data['wcf_shipping_state'] : ''; |
| 167 |
$country_code = isset($other_data['wcf_shipping_country']) ? $other_data['wcf_shipping_country'] : ''; |
| 168 |
if('' == $state_code || '' == $country_code){ |
| 169 |
return ''; |
| 170 |
} |
| 171 |
return WC()->countries->states[$country_code][$state_code]; |
| 172 |
} |
| 173 |
), |
| 174 |
'shipping_country' => array( |
| 175 |
'preview' => 'US', |
| 176 |
'value' => function ($other_data) { |
| 177 |
$country_code = isset($other_data['wcf_shipping_country']) ? $other_data['wcf_shipping_country'] : ''; |
| 178 |
if('' == $country_code){ |
| 179 |
return ''; |
| 180 |
} |
| 181 |
return WC()->countries->countries[$country_code]; |
| 182 |
} |
| 183 |
) |
| 184 |
|
| 185 |
); |
| 186 |
|
| 187 |
foreach ($wcar_other_fields as $field => $field_data) { |
| 188 |
if (isset($field_data['label'])) { |
| 189 |
$label = $field_data['label']; |
| 190 |
} else { |
| 191 |
$label = ucfirst( str_replace('_', ' ', $field) ); |
| 192 |
} |
| 193 |
|
| 194 |
$merge_tags['WooCommerce Cart Abandonment Recovery'][] = array( |
| 195 |
'id' => 'wcar_cart_customer_' . $field, |
| 196 |
'label' => $label, |
| 197 |
'preview_value' => isset($field_data['preview']) ? $field_data['preview'] : '', |
| 198 |
'return_type' => isset($field_data['return_type']) ? $field_data['return_type'] : 'text', |
| 199 |
'value' => function ($checkout_details) use ($field, $field_data) { |
| 200 |
$other_fields = isset($checkout_details['other_fields']) ? $checkout_details['other_fields'] : ''; |
| 201 |
$other_fields = maybe_unserialize($other_fields); |
| 202 |
if(empty($other_fields)){ |
| 203 |
return ''; |
| 204 |
} |
| 205 |
|
| 206 |
if (isset($field_data['value'])) { |
| 207 |
$value = $field_data['value']($other_fields); |
| 208 |
} else { |
| 209 |
$value = $other_fields['wcf_' . $field]; |
| 210 |
} |
| 211 |
return html_entity_decode(sanitize_text_field($value)); |
| 212 |
} |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
return $merge_tags; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Add recipient fields |
| 221 |
*/ |
| 222 |
public static function wcar_recipient_fields($recipient_fields){ |
| 223 |
$recipient_fields['WooCommerce Cart Abandonment Recovery'] = array( |
| 224 |
array( |
| 225 |
'id' => 'wcar_cart_customer_billing_phone', |
| 226 |
'label' => 'Customer billing phone number', |
| 227 |
'value' => function ($checkout_details) { |
| 228 |
$other_fields = isset($checkout_details['other_fields']) ? $checkout_details['other_fields'] : ''; |
| 229 |
$other_fields = maybe_unserialize($other_fields); |
| 230 |
if(empty($other_fields)){ |
| 231 |
return ''; |
| 232 |
} |
| 233 |
$phone_number = $other_fields['wcf_phone_number']; |
| 234 |
$location = isset($other_fields['wcf_location']) ? explode(', ', $other_fields['wcf_location']) : ''; |
| 235 |
$country_code = isset($location[0]) ? $location[0] : ''; |
| 236 |
$phone_number = Notifier_Woocommerce::get_formatted_phone_number($phone_number, $country_code); |
| 237 |
return html_entity_decode(sanitize_text_field($phone_number)); |
| 238 |
} |
| 239 |
) |
| 240 |
); |
| 241 |
|
| 242 |
return $recipient_fields; |
| 243 |
} |
| 244 |
|
| 245 |
} |
| 246 |
|