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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
if ( !class_exists('Wcal_Personal_Data_Export' ) ) {
class Wcal_Personal_Data_Export {
public function __construct() {
add_filter( 'wp_privacy_personal_data_exporters', array( &$this, 'wcal_exporter_array' ), 6 );
}
public static function wcal_exporter_array( $exporters = array() ) {
$exporter_list = array();
$exporter_list[ 'wcal_carts' ] = array(
'exporter_friendly_name' => __( 'Abandoned & Recovered Carts', 'woocommerce-abandoned-cart' ),
'callback' => array( 'Wcal_Personal_Data_Export', 'wcal_data_exporter' )
);
$exporters = array_merge( $exporters, $exporter_list );
return $exporters;
}
static function wcal_data_exporter( $email_address, $page ) {
global $wpdb;
$done = false;
$page = (int) $page;
$user = get_user_by( 'email', $email_address );
$data_to_export = array();
$blank_cart_info = '{"cart":[]}';
$blank_cart_info_guest = '[]';
$blank_cart = '""';
$user_id = $user ? (int) $user->ID : 0;
if( $user_id > 0 ) {
$cart_query = "SELECT id FROM `" . $wpdb->prefix . 'ac_abandoned_cart_history_lite' . "`
WHERE user_id = %d AND
user_type = 'REGISTERED' AND abandoned_cart_info NOT LIKE '%$blank_cart_info%' AND abandoned_cart_info NOT LIKE '$blank_cart'";
$cart_ids = $wpdb->get_results( $wpdb->prepare( $cart_query, $user_id ) );
} else {
$guest_query = "SELECT id FROM `" . $wpdb->prefix . 'ac_guest_abandoned_cart_history_lite' . "`
WHERE email_id = %s";
$guest_user_ids = $wpdb->get_results( $wpdb->prepare( $guest_query, $email_address ) );
if( count( $guest_user_ids ) == 0 )
return;
$cart_ids = array();
foreach( $guest_user_ids as $ids ) {
$cart_query = "SELECT id, abandoned_cart_info AS cart_info FROM `" . $wpdb->prefix . 'ac_abandoned_cart_history_lite' . "`
WHERE user_id = %d AND
user_type = 'GUEST'";
$cart_data = $wpdb->get_results( $wpdb->prepare( $cart_query, $ids->id ) );
$cart_ids = array_merge( $cart_ids, $cart_data );
}
}
if ( 0 < count( $cart_ids ) ) {
$cart_chunks = array_chunk( $cart_ids, 10, true );
$cart_export = isset( $cart_chunks[ $page - 1 ] ) ? $cart_chunks[ $page - 1 ] : array();
if( count( $cart_export ) > 0 ) {
foreach ( $cart_export as $abandoned_ids ) {
$cart_id = $abandoned_ids->id;
if( count( $abandoned_ids->id ) > 0 ) {
$data_to_export[] = array(
'group_id' => 'wcal_carts',
'group_label' => __( 'Abandoned Carts', 'woocommerce-abandoned-cart' ),
'item_id' => 'cart-' . $cart_id,
'data' => self::get_cart_data( $cart_id ),
);
}
}
$done = $page > count( $cart_chunks );
} else {
$done = true;
}
} else {
$done = true;
}
return array(
'data' => $data_to_export,
'done' => $done,
);
}
static function get_cart_data( $abandoned_id ) {
$personal_data = array();
global $wpdb;
$cart_query = "SELECT * FROM `" . $wpdb->prefix . 'ac_abandoned_cart_history_lite' . "`
WHERE id = %d";
$cart_details = $wpdb->get_results( $wpdb->prepare( $cart_query, $abandoned_id ) );
$cart_details = $cart_details[0];
$user_id = $cart_details->user_id;
$user_type = $cart_details->user_type;
if( $user_type == 'GUEST' ) {
$cart_details_to_export = apply_filters( 'wcal_personal_export_cart_details_prop', array(
'cart_id' => __( 'Abandoned Cart ID', 'woocommerce-abandoned-cart' ),
'date_created' => __( 'Abandoned Date', 'woocommerce-abandoned-cart' ),
'cart_status' => __( 'Abandoned Cart Status', 'woocommerce-abandoned-cart' ),
'total' => __( 'Cart Total', 'woocommerce-abandoned-cart' ),
'items' => __( 'Items Present', 'woocommerce-abandoned-cart' ),
'session_id' => __( 'Session ID', 'woocommerce-abandoned-cart' ),
'formatted_billing_address' => __( 'Billing Address', 'woocommerce-abandoned-cart' ),
'billing_email' => __( 'Email Address', 'woocommerce-abandoned-cart' ),
), $abandoned_id );
} else {
$cart_details_to_export = apply_filters( 'wcal_personal_export_cart_details_prop', array(
'cart_id' => __( 'Abandoned Cart ID', 'woocommerce-abandoned-cart' ),
'date_created' => __( 'Abandoned Date', 'woocommerce-abandoned-cart' ),
'cart_status' => __( 'Abandoned Cart Status', 'woocommerce-abandoned-cart' ),
'total' => __( 'Cart Total', 'woocommerce-abandoned-cart' ),
'items' => __( 'Items Present', 'woocommerce-abandoned-cart' ),
'formatted_billing_address' => __( 'Billing Address', 'woocommerce-abandoned-cart' ),
'billing_email' => __( 'Email Address', 'woocommerce-abandoned-cart' ),
), $abandoned_id );
}
$cart_data = json_decode( stripslashes( $cart_details->abandoned_cart_info ) );
$cart_info = $cart_data->cart;
if( count( $cart_info ) > 0 ) {
$cart_details_formatted = self::wcal_get_cart_details_export( $cart_info );
}
if( $user_type == 'GUEST' ) {
$guest_details = self::wcal_get_guest_personal_info( $user_id );
}
foreach( $cart_details_to_export as $prop => $name ) {
switch( $prop ) {
case 'cart_id':
$value = $cart_details->id;
break;
case 'date_created':
$value = date( 'Y-m-d H:i:s', $cart_details->abandoned_cart_time );
break;
case 'cart_status':
$cart_ignored = $cart_details->cart_ignored;
switch( $cart_ignored ) {
case '0':
$value = $cart_details->recovered_cart > 0 ? __( "Cart Recovered - Order #", 'woocommerce-abandoned-cart' ) . $cart_details->recovered_cart : __( 'Abandoned', 'woocommerce-abandoned-cart' );
break;
case '1':
$value = $cart_details->recovered_cart > 0 ? __( "Cart Recovered - Order #", 'woocommerce-abandoned-cart' ) . $cart_details->recovered_cart : __( 'Abandoned but new cart created', 'woocommerce-abandoned-cart' );
break;
case '2':
$value = __( 'Abandoned - Order Unpaid (Order #', 'woocommerce-abandoned-cart') . $cart_details->recovered_cart . ")";
break;
}
break;
case 'total':
$total = 0;
if( count( $cart_info ) > 0 ) {
foreach( $cart_info as $k => $v ) {
$total += $cart_details_formatted[$k][ 'item_total' ];
}
}
$value = wc_price( $total );
break;
case 'items':
$value = '';
if( count( $cart_info ) > 0 ) {
foreach( $cart_info as $k => $v ) {
$product_name = $cart_details_formatted[$k][ 'product_name' ];
$qty = $cart_details_formatted[$k][ 'qty' ];
$value .= ( $value == '' ) ? "$product_name x $qty" : ", $product_name x $qty";
}
}
break;
case 'formatted_billing_address':
if( $user_type == 'REGISTERED' ) {
$billing = wcal_common::wcal_get_billing_details( $user_id );
$value = get_user_meta( $user_id, 'billing_first_name', true );
$value .= ' ' . get_user_meta( $user_id, 'billing_last_name', true );
if( count( $billing ) > 0 ) {
foreach( $billing as $details ) {
if( $details != '' ) {
$value .= ",$details ";
}
}
}
} elseif ( $user_type == 'GUEST' ) {
if( count( $guest_details ) > 0 ) {
$value = $guest_details->billing_first_name;
$value .= ' ' . $guest_details->billing_last_name;
}
}
break;
case 'billing_email':
if( $user_type == 'REGISTERED' ) {
$value = get_user_meta( $user_id, $prop, true );
} else if( $user_type == 'GUEST' ) {
if( count( $guest_details ) > 0 ) {
$value = $guest_details->$prop;
}
}
break;
default:
$value = ( isset( $cart_details->$prop ) ) ? $cart_details->$prop : '';
break;
}
$value = apply_filters( 'wcal_personal_export_cart_details_prop_value', $value, $prop, $cart_details );
$personal_data[] = array(
'name' => $name,
'value' => $value,
);
}
$personal_data = apply_filters( 'wcal_personal_data_cart_details_export', $personal_data, $cart_details );
return $personal_data;
}
static function wcal_get_guest_personal_info( $user_id ) {
global $wpdb;
$guest_details = array();
$guest_query = "SELECT billing_first_name, billing_last_name, email_id AS billing_email, phone AS billing_phone FROM `" . $wpdb->prefix . 'ac_guest_abandoned_cart_history_lite' . "`
WHERE id = %d";
$guest_details = $wpdb->get_results( $wpdb->prepare( $guest_query, $user_id ) );
if( is_array( $guest_details ) && count( $guest_details ) > 0 ) {
$guest_details = $guest_details[0];
}
return $guest_details;
}
static function wcal_get_cart_details_export( $cart_info ) {
$cart_details = array();
if( count( $cart_info ) > 0 ) {
$cart_total = 0;
foreach( $cart_info as $k => $item_detail ) {
$qty = $item_detail->quantity;
$product_id = $item_detail->product_id;
$prod_obj = wc_get_product( $product_id );
$product_name = $prod_obj->get_name();
if( isset( $item_detail->variation_id ) && $item_detail->variation_id > 0 ) {
$variation_id = $item_detail->variation_id;
$variation = wc_get_product( $variation_id );
$variation_name = $variation->get_name();
$product_name = $variation_name;
}
$item_total = $item_detail->line_total;
if ( $item_detail->line_subtotal_tax > 0 ) {
$item_total += $item_detail->line_subtotal_tax;
}
$cart_details[ $k ][ 'qty' ] = $qty;
$cart_details[ $k ][ 'product_name' ] = $product_name;
$cart_details[ $k ][ 'item_total' ] = $item_total;
$cart_total += $item_total;
}
$cart_details[ 'total' ] = $cart_total;
}
return $cart_details;
}
}
$Wcal_Personal_Data_Export = new Wcal_Personal_Data_Export();
}
?>