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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
<?php
if ( !defined( 'ABSPATH' ) ) {
exit;
}
class Wcap_Dashboard_Report {
function get_begin_of_month( $selected_data_range, $user_start_date ) {
$begin_date = '';
switch ( $selected_data_range ){
case 'this_month':
$begin_date = mktime(00, 01, 01, date("n"), 1);
break;
case 'last_month':
$begin_date = mktime(00, 01, 01, date("n") - 1, 1);
break;
case 'this_quarter':
$current_month = date('m');
$current_year = date('Y');
if($current_month>=1 && $current_month<=3) {
$begin_date = strtotime('1-January-'.$current_year. '00:01:01' );
}
else if($current_month>=4 && $current_month<=6) {
$begin_date = strtotime('1-April-'.$current_year. '00:01:01' );
}
else if($current_month>=7 && $current_month<=9) {
$begin_date = strtotime('1-July-'.$current_year. '00:01:01' );
}
else if($current_month>=10 && $current_month<=12) {
$begin_date = strtotime('1-October-'.$current_year. '00:01:01' );
}
break;
case 'last_quarter':
$current_month = date('m');
$current_year = date('Y');
if( $current_month >= 1 && $current_month <= 3 ) {
$begin_date = strtotime( '1-October-'.($current_year-1). '00:01:01' );
}
else if( $current_month >= 4 && $current_month <= 6 ) {
$begin_date = strtotime( '1-January-'.$current_year . '00:01:01' );
}
else if( $current_month >= 7 && $current_month <= 9 ) {
$begin_date = strtotime( '1-April-'.$current_year. '00:01:01' );
}
else if( $current_month >= 10 && $current_month <= 12 ) {
$begin_date = strtotime( '1-July-'.$current_year. '00:01:01' );
}
break;
case 'this_year':
$begin_date = mktime( 00, 01, 01, 1, 1, date('Y') );;
break;
case 'last_year':
$begin_date = mktime( 00, 01, 01, 1, 1, date('Y')-1 );
break;
case 'other':
$explode_start_date = explode ( "-", $user_start_date );
$month = $explode_start_date[1];
$date = $explode_start_date[2];
$year = $explode_start_date[0];
$begin_date = mktime( 00, 01, 01, $month, $date, $year );
break;
default:
$begin_date = time();
}
return $begin_date;
}
function get_end_of_month ( $selected_data_range, $user_end_date ){
$end_date = '';
switch ( $selected_data_range ){
case 'this_month':
$end_date = mktime( 23, 59, 59, date("n"), date("t") );
break;
case 'last_month':
$end_date = mktime( 23, 59, 59, date("n") - 1, date("t") - 1 );
break;
case 'this_quarter':
$current_month = date('m');
$current_year = date('Y');
if( $current_month >= 1 && $current_month <= 3) {
$end_date = strtotime( '31-March-'.$current_year.'23:59:59' );
}
else if( $current_month >= 4 && $current_month <=6 ) {
$end_date = strtotime( '30-July-'.$current_year.'23:59:59' );
}
else if( $current_month >= 7 && $current_month <= 9 ) {
$end_date = strtotime( '30-September-'.$current_year.'23:59:59' );
}
else if( $current_month >= 10 && $current_month <= 12) {
$end_date = strtotime( '1-January-'.( $current_year + 1 ).'23:59:59');
}
break;
case 'last_quarter':
$current_month = date('m');
$current_year = date('Y');
if( $current_month >= 1 && $current_month <= 3 ) {
$end_date = strtotime( '1-January-'.$current_year.'23:59:59' );
}
else if( $current_month >= 4 && $current_month <= 6 ) {
$end_date = strtotime( '31-March-'.$current_year.'23:59:59' );
}
else if( $current_month >= 7 && $current_month <= 9 ) {
$end_date = strtotime( '30-June-'.$current_year.'23:59:59' );
}
else if( $current_month >= 10 && $current_month <= 12 ) {
$end_date = strtotime( '30-September-'.$current_year.'23:59:59' );
}
break;
case 'this_year':
$end_date = mktime( 23, 59, 59, date('m'), date('d'), date('y') );
break;
case 'last_year':
$end_date = mktime( 23, 59, 59, 12, 31, date('Y')-1 );
break;
case 'other':
$explode_end_date = explode ( "-", $user_end_date );
$month = $explode_end_date[1];
$date = $explode_end_date[2];
$year = $explode_end_date[0];
$end_date = mktime( 23, 59, 59, $month, $date, $year );
break;
default:
$end_date = time();
}
return $end_date;
}
function get_this_month_amount_reports( $type, $selected_data_range, $start_date, $end_date ) {
$count_month = 0;
$begin_of_month = $this->get_begin_of_month ( $selected_data_range, $start_date );
$end_of_month = $this->get_end_of_month ( $selected_data_range, $end_date );
switch ( $type ){
case 'recover':
$count_month = $this->get_current_month_recovered_amount ( $begin_of_month, $end_of_month);
break;
case 'wc_total_sales':
$count_month = $this->get_wc_total_sales ( $begin_of_month, $end_of_month);
break;
}
return $count_month;
}
function get_this_month_number_reports ( $type, $selected_data_range, $start_date, $end_date ) {
$count = 0;
$begin_of_month = $this->get_begin_of_month ( $selected_data_range, $start_date );
$end_of_month = $this->get_end_of_month ( $selected_data_range, $end_date );
switch ( $type ) {
case 'abandoned':
$count = $this->get_current_month_abandoned_count ( $begin_of_month, $end_of_month);
break;
case 'recover':
$count = $this->get_current_month_recovered_count ( $begin_of_month, $end_of_month);
break;
}
return $count;
}
function get_this_month_total_vs_abandoned_order( $type, $selected_data_range, $start_date, $end_date ) {
$count = 0;
$begin_of_month = $this->get_begin_of_month ( $selected_data_range, $start_date );
$end_of_month = $this->get_end_of_month ( $selected_data_range, $end_date );
switch ( $type ) {
case 'abandoned':
$count = $this->get_current_month_abandoned_count( $begin_of_month, $end_of_month );
break;
case 'wc_total_orders':
$count = $this->get_this_month_wc_total_order_count( $begin_of_month, $end_of_month );
break;
}
return $count;
}
function wcap_get_email_report ( $type, $selected_data_range, $user_start_date, $user_end_date ){
$count = 0;
$begin_date = $this->get_begin_of_month ( $selected_data_range, $user_start_date );
$end_date = $this->get_end_of_month ( $selected_data_range, $user_end_date );
$start_date_db = date( 'Y-m-d H:i:s', $begin_date );
$end_date_db = date( 'Y-m-d H:i:s', $end_date );
switch ( $type ) {
case 'total_sent':
$count = $this->wcap_get_total_email_sent_count ( $start_date_db, $end_date_db);
break;
case 'total_opened':
$count = $this->wcap_get_total_emails_opened ( $start_date_db, $end_date_db);
break;
case 'total_clicked':
$count = $this->wcap_get_total_emails_clicked ( $start_date_db, $end_date_db);
break;
}
return $count;
}
function wcap_get_total_email_sent_count ( $start_date_db, $end_date_db ) {
global $wpdb;
$query_ac_sent = "SELECT COUNT(wpsh.id) FROM " . WCAP_EMAIL_SENT_HISTORY_TABLE . " as wpsh
LEFT JOIN ".WCAP_ABANDONED_CART_HISTORY_TABLE." AS wpac ON wpsh.abandoned_order_id = wpac.id
WHERE wpsh.abandoned_order_id = wpac.id
AND
wpsh.sent_time >= '$start_date_db'
AND
wpsh.sent_time <= '$end_date_db' ";
$total_sent_email_count = $wpdb->get_var( $query_ac_sent );
return $total_sent_email_count;
}
function wcap_get_total_emails_opened ( $start_date_db, $end_date_db ) {
global $wpdb;
$query_ac_opened = "SELECT COUNT(DISTINCT wpoe.email_sent_id) FROM " . WCAP_EMAIL_OPENED_TABLE . " as wpoe
LEFT JOIN ". WCAP_EMAIL_SENT_HISTORY_TABLE." AS wpsh ON wpsh.id = wpoe.email_sent_id
WHERE
time_opened >= '" . $start_date_db . "'
AND
time_opened <= '" . $end_date_db . "'
AND
wpsh.id = wpoe.email_sent_id ";
$wcap_opened_emails = $wpdb->get_var( $query_ac_opened );
return $wcap_opened_emails;
}
function wcap_get_total_emails_clicked( $start_date_db, $end_date_db ) {
global $wpdb;
$query_ac_clicked = "SELECT COUNT(DISTINCT wplc.email_sent_id) FROM " . WCAP_EMAIL_CLICKED_TABLE . " as wplc
LEFT JOIN ".WCAP_EMAIL_SENT_HISTORY_TABLE." AS wpsh ON wplc.email_sent_id = wpsh.id
WHERE wplc.time_clicked >= '" . $start_date_db . "'
AND
wplc.time_clicked <= '" . $end_date_db . "'
ORDER BY wplc.id DESC ";
$wcap_opened_clicked = $wpdb->get_var( $query_ac_clicked );
return $wcap_opened_clicked;
}
function get_wc_total_sales( $begin_of_month, $end_of_month ) {
global $wpdb;
$count_month = 0;
$begin_date_of_month = date( 'Y-m-d H:i:s', $begin_of_month );
$end_date_of_month = date( 'Y-m-d H:i:s', $end_of_month );
$order_totals = $wpdb->get_row( "
SELECT SUM(meta.meta_value) AS total_sales, COUNT(posts.ID) AS total_orders FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
WHERE meta.meta_key = '_order_total'
AND posts.post_type = 'shop_order'
AND posts.post_date >= '$begin_date_of_month'
AND posts.post_date <= '$end_date_of_month'
AND posts.post_status IN ( '" . implode( "','", array( 'wc-completed', 'wc-processing', 'wc-on-hold' ) ) . "' )
" ) ;
$count_month = $order_totals->total_sales == null ? 0 : $order_totals->total_sales;
return $count_month;
}
function get_current_month_recovered_amount ( $begin_of_month, $end_of_month ){
global $wpdb;
$blank_cart_info = '{"cart":[]}';
$blank_cart_info_guest = '[]';
$ac_results = array();
$query_ac = "SELECT id, recovered_cart FROM " . WCAP_ABANDONED_CART_HISTORY_TABLE . "
WHERE abandoned_cart_info NOT LIKE %s
AND
abandoned_cart_info NOT LIKE %s
AND
abandoned_cart_time >= %d
AND
abandoned_cart_time <= %d
AND
recovered_cart > 0
AND
wcap_trash = ''
ORDER BY recovered_cart desc";
$ac_results = $wpdb->get_results( $wpdb->prepare( $query_ac, $blank_cart_info, $blank_cart_info_guest, $begin_of_month, $end_of_month ) );
$recovered_order_total = 0;
$wc_get_price_decimals = wc_get_price_decimals();
$this->total_recover_amount = round( $recovered_order_total, $wc_get_price_decimals ) ;
$count_month = 0;
$table_data = "";
foreach ( $ac_results as $key => $value ) {
if( 0 != $value->recovered_cart ) {
$abandoned_order_id = $value->id;
$recovered_id = $value->recovered_cart;
$rec_order = get_post_meta( $recovered_id, '_order_total' );
$recovered_order_total = 0;
if ( isset( $rec_order[0] ) ) {
$wcap_recovered_amount = $rec_order[0];
$recovered_order_total = $wcap_recovered_amount;
}
$count_month = round( ( $recovered_order_total + $count_month ) , $wc_get_price_decimals );
}
}
return $count_month;
}
function get_current_month_abandoned_amount( $begin_of_month, $end_of_month ) {
global $wpdb;
$count_month = 0;
$start_date = $begin_of_month;
$end_date = $end_of_month;
$blank_cart_info = '{"cart":[]}';
$blank_cart_info_guest = '[]';
$query_ac_carts = "SELECT abandoned_cart_info FROM " . WCAP_ABANDONED_CART_HISTORY_TABLE . "
WHERE abandoned_cart_info NOT LIKE %s
AND
abandoned_cart_info NOT LIKE %s
AND
abandoned_cart_time >= %d
AND
abandoned_cart_time <= %d
AND
wcap_trash = '' ";
$ac_carts_results = $wpdb->get_results( $wpdb->prepare( $query_ac_carts, $blank_cart_info, $blank_cart_info_guest, $start_date, $end_date ) );
$count_carts = $total_value = 0;
foreach ( $ac_carts_results as $key => $value ) {
$count_carts += 1;
$cart_detail = json_decode( $value->abandoned_cart_info );
$product_details = array();
if( isset( $cart_detail->cart ) ){
$product_details = $cart_detail->cart;
}
$line_total = 0;
if ( isset( $product_details ) && count( $product_details ) > 0 && $product_details != false ) {
foreach ( $product_details as $k => $v ) {
if( $v->line_subtotal_tax != 0 && $v->line_subtotal_tax > 0 ) {
$line_total = $line_total + $v->line_total + $v->line_subtotal_tax;
} else {
$line_total = $line_total + $v->line_total;
}
}
}
$total_value += $line_total;
}
$count_month = round( $total_value, wc_get_price_decimals() );
return $count_month;
}
function get_current_month_abandoned_count ( $begin_of_month, $end_of_month ) {
global $wpdb;
$count_month = 0;
$blank_cart_info = '{"cart":[]}';
$blank_cart_info_guest = '[]';
$ac_cutoff_time = get_option( 'ac_cart_abandoned_time' );
$cut_off_time = $ac_cutoff_time * 60;
$current_time = current_time( 'timestamp' );
$compare_time = $current_time - $cut_off_time;
$ac_cutoff_time_guest = get_option( 'ac_cart_abandoned_time_guest' );
$cut_off_time_guest = $ac_cutoff_time_guest * 60;
$current_time = current_time ('timestamp');
$compare_time_guest = $current_time - $cut_off_time_guest;
$query_abandoned = "SELECT COUNT(id) FROM `".WCAP_ABANDONED_CART_HISTORY_TABLE."`
WHERE ( user_type = 'REGISTERED' AND abandoned_cart_time >= $begin_of_month AND abandoned_cart_time <= $end_of_month AND abandoned_cart_info NOT LIKE '%$blank_cart_info%' AND abandoned_cart_time <= '$compare_time' AND wcap_trash = '' )
OR ( user_type = 'GUEST' AND abandoned_cart_time >= $begin_of_month AND abandoned_cart_time <= $end_of_month AND abandoned_cart_info NOT LIKE '$blank_cart_info_guest' AND abandoned_cart_info NOT LIKE '%$blank_cart_info%' AND abandoned_cart_time <= '$compare_time_guest' AND wcap_trash = '' )
";
$count_month = $wpdb->get_var( $query_abandoned );
return $count_month;
}
function get_current_month_recovered_count( $begin_of_month, $end_of_month ) {
global $wpdb;
$count_month = 0;
$blank_cart_info = '{"cart":[]}';
$blank_cart_info_guest = '[]';
$query_recover = "SELECT COUNT(wach.recovered_cart) FROM `".WCAP_ABANDONED_CART_HISTORY_TABLE."` as wach
LEFT JOIN ".$wpdb->prefix."posts AS wposts ON wach.recovered_cart = wposts.ID
WHERE
wach.abandoned_cart_time >= $begin_of_month
AND
wach.abandoned_cart_time <= $end_of_month
AND
wach.recovered_cart != 0
AND
wach.abandoned_cart_info NOT LIKE '%$blank_cart_info%'
AND
wach.abandoned_cart_info NOT LIKE '$blank_cart_info_guest'
AND
wach.wcap_trash = ''
AND
wach.recovered_cart = wposts.ID ";
$count_month = $wpdb->get_var( $query_recover );
return $count_month;
}
function get_this_month_wc_total_order_count( $begin_of_month, $end_of_month ) {
global $wpdb;
$count_month = 0;
$begin_date_of_month = date( 'Y-m-d H:i:s', $begin_of_month );
$end_date_of_month = date( 'Y-m-d H:i:s', $end_of_month );
$sales_query = "SELECT COUNT(ID) FROM {$wpdb->posts} as posts
WHERE posts.post_type = 'shop_order'
AND
posts.post_date >= '$begin_date_of_month'
AND
posts.post_date <= '$end_date_of_month'
AND
posts.post_status IN ( '" . implode( "','", array( 'wc-completed', 'wc-processing', 'wc-on-hold' ) ) . "' ) ";
$count_month = $wpdb->get_var( $sales_query );
return $count_month;
}
function wcap_get_total_email_sent_for_template( $wcap_template_id, $selected_data_range, $start_date, $end_date ) {
global $wpdb;
$begin_of_month = $this->get_begin_of_month ( $selected_data_range, $start_date );
$end_of_month = $this->get_end_of_month ( $selected_data_range, $end_date );
$begin_date_of_month = date( 'Y-m-d H:i:s', $begin_of_month );
$end_date_of_month = date( 'Y-m-d H:i:s', $end_of_month );
$query_no_emails = "SELECT COUNT(id) FROM " . WCAP_EMAIL_SENT_HISTORY_TABLE . "
WHERE template_id = $wcap_template_id
AND
sent_time >= '$begin_date_of_month'
AND
sent_time <= '$end_date_of_month'";
$wcap_emails_sent_count = $wpdb->get_var( $query_no_emails );
return $wcap_emails_sent_count;
}
function wcap_get_total_email_open_for_template( $wcap_template_id, $selected_data_range, $start_date, $end_date ){
global $wpdb;
$begin_of_month = $this->get_begin_of_month ( $selected_data_range, $start_date );
$end_of_month = $this->get_end_of_month ( $selected_data_range, $end_date );
$begin_date_of_month = date( 'Y-m-d H:i:s', $begin_of_month );
$end_date_of_month = date( 'Y-m-d H:i:s', $end_of_month );
$query_ac_opened = "SELECT COUNT( DISTINCT wpoe.email_sent_id )
FROM " . WCAP_EMAIL_OPENED_TABLE . " AS wpoe
LEFT JOIN ".WCAP_EMAIL_SENT_HISTORY_TABLE." AS wpsh ON wpsh.id = wpoe.email_sent_id
WHERE time_opened >= '$begin_date_of_month'
AND time_opened <= '$end_date_of_month'
AND wpsh.id = wpoe.email_sent_id
AND wpsh.template_id = '$wcap_template_id' ";
$wcap_emails_opened_count = $wpdb->get_var( $query_ac_opened );
return $wcap_emails_opened_count;
}
function wcap_get_total_email_click_for_template ( $wcap_template_id, $selected_data_range, $start_date, $end_date ){
global $wpdb;
$begin_of_month = $this->get_begin_of_month ( $selected_data_range, $start_date );
$end_of_month = $this->get_end_of_month ( $selected_data_range, $end_date );
$begin_date_of_month = date( 'Y-m-d H:i:s', $begin_of_month );
$end_date_of_month = date( 'Y-m-d H:i:s', $end_of_month );
$query_ac_clicked = "SELECT COUNT( DISTINCT wplc.email_sent_id) FROM " . WCAP_EMAIL_CLICKED_TABLE . " as wplc
LEFT JOIN ".WCAP_EMAIL_SENT_HISTORY_TABLE." AS wpsh ON wpsh.id = wplc.email_sent_id
WHERE wpsh.template_id = $wcap_template_id
AND
wpsh.sent_time >= '$begin_date_of_month'
AND
wpsh.sent_time <= '$end_date_of_month'
AND wplc.email_sent_id IN ( SELECT email_sent_id FROM " . WCAP_EMAIL_OPENED_TABLE . " )
";
$wcap_emails_clicked_count = $wpdb->get_var( $query_ac_clicked );
return $wcap_emails_clicked_count;
}
function wcap_get_total_email_recover_for_template( $wcap_template_id, $selected_data_range, $start_date, $end_date ) {
global $wpdb;
$begin_of_month = $this->get_begin_of_month ( $selected_data_range, $start_date );
$end_of_month = $this->get_end_of_month ( $selected_data_range, $end_date );
$begin_date_of_month = date( 'Y-m-d H:i:s', $begin_of_month );
$end_date_of_month = date( 'Y-m-d H:i:s', $end_of_month );
$query_recovered_orders = "SELECT COUNT(id) FROM " . WCAP_EMAIL_SENT_HISTORY_TABLE . " WHERE
template_id = $wcap_template_id
AND
recovered_order != '0'
AND
sent_time >= '$begin_date_of_month'
AND
sent_time <= '$end_date_of_month'
AND id IN ( SELECT email_sent_id FROM " . WCAP_EMAIL_OPENED_TABLE . " )
";
$number_order_recovered_count = $wpdb->get_var( $query_recovered_orders );
return $number_order_recovered_count;
}
function wcap_get_atc_data_of_range( $selected_data_range, $start_date, $end_date ) {
global $wpdb;
$begin_of_month = $this->get_begin_of_month ( $selected_data_range, $start_date );
$end_of_month = $this->get_end_of_month ( $selected_data_range, $end_date );
$blank_cart_info = '{"cart":[]}';
$blank_cart_info_guest = '[]';
$wcap_post_ac_atc_stats = array();
$ac_cutoff_time = get_option( 'ac_cart_abandoned_time' );
$cut_off_time = $ac_cutoff_time * 60;
$current_time = current_time( 'timestamp' );
$compare_time = $current_time - $cut_off_time;
$ac_cutoff_time_guest = get_option( 'ac_cart_abandoned_time_guest' );
$cut_off_time_guest = $ac_cutoff_time_guest * 60;
$current_time = current_time ('timestamp');
$compare_time_guest = $current_time - $cut_off_time_guest;
$query_abandoned = "SELECT * FROM `".WCAP_ABANDONED_CART_HISTORY_TABLE."` WHERE ( user_type = 'REGISTERED' AND abandoned_cart_time >= $begin_of_month AND abandoned_cart_time <= $end_of_month AND abandoned_cart_info NOT LIKE '%$blank_cart_info%' AND abandoned_cart_time <= '$compare_time' AND wcap_trash = '' ) OR ( user_type = 'GUEST' AND abandoned_cart_time >= $begin_of_month AND abandoned_cart_time <= $end_of_month AND abandoned_cart_info NOT LIKE '$blank_cart_info_guest' AND abandoned_cart_info NOT LIKE '%$blank_cart_info%' AND abandoned_cart_time <= '$compare_time_guest' AND wcap_trash = '' )";
$ac_carts_results = $wpdb->get_results($query_abandoned);
if ( count( $ac_carts_results ) > 0 ){
$wcap_start_ac_id = reset( $ac_carts_results );
$wcap_end_ac_id = end ( $ac_carts_results );
$wcap_post_ac_carts_stats = "SELECT post_id, meta_value FROM " . $wpdb->prefix . "postmeta WHERE meta_key = 'wcap_atc_report' AND post_id >= %d AND post_id <= %d ";
$wcap_post_ac_carts_stats_results = $wpdb->get_results( $wpdb->prepare( $wcap_post_ac_carts_stats, $wcap_start_ac_id->id , $wcap_end_ac_id->id ) );
$wcap_atc_open_count = 0;
$wcap_atc_email_given_count = 0;
$wcap_atc_not_email_given_count = 0;
foreach( $wcap_post_ac_carts_stats_results as $wcap_post_ac_carts_stats_results_key => $wcap_post_ac_carts_stats_results_value ) {
$wcap_ac_atc_data = $wcap_post_ac_carts_stats_results_value->meta_value;
$wcap_ac_atc_data_decode = unserialize( $wcap_ac_atc_data );
if( 'yes' == $wcap_ac_atc_data_decode['wcap_atc_open'] ) {
$wcap_atc_open_count++;
}
if( 'yes' == $wcap_ac_atc_data_decode['wcap_atc_action'] ) {
$wcap_atc_email_given_count++;
} else if ( 'no' == $wcap_ac_atc_data_decode['wcap_atc_action'] ) {
$wcap_atc_not_email_given_count++;
}
}
$wcap_post_ac_atc_stats = array( "wcap_atc_open" => $wcap_atc_open_count, "wcap_has_email" => $wcap_atc_email_given_count, "wcap_not_has_email" => $wcap_atc_not_email_given_count );
}
return $wcap_post_ac_atc_stats;
}
}