Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hook Reference
  • Docs Home

Packages

  • Abandoned-Cart-Pro-for-WooCommerce
    • Admin
      • Action
      • Activate-plugin
      • Component
      • I18N
      • Import-Pro
      • License
      • Menu
      • Notices
      • Popup-Modal
        • Cart-Detail
      • Recover
      • Report
      • Setting
      • Settings
      • SMS
      • Tab
      • Template
    • Ajax-Functions
    • Classes
    • Common-Functions
    • Database-Layer
    • Encrypt-Decrypt-Data
    • Frontend
      • Cart-Capture
      • Cart-Populate
      • Coupon
      • Place-Order
    • Setting
    • Update
  • None

Classes

  • EDD_AC_WOO_Plugin_Updater
  • Wcap_Abandoned_Cart_Details
  • Wcap_Abandoned_Cart_List
  • Wcap_Abandoned_Orders_Table
  • Wcap_Abandoned_Trash_Orders_Table
  • Wcap_Actions
  • Wcap_Actions_Handler
  • Wcap_Activate_Plugin
  • wcap_active_license_notice
  • Wcap_Add_Cart_Popup_Modal
  • Wcap_Add_Settings
  • Wcap_Admin_Recovery
  • Wcap_Adv_settings
  • Wcap_Aes
  • Wcap_Aes_Ctr
  • Wcap_Ajax
  • Wcap_All_Component
  • Wcap_Atc_Dashboard
  • Wcap_Cart_Updated
  • Wcap_Common
  • Wcap_Coupon
  • Wcap_Dashboard
  • Wcap_Dashboard_Report
  • Wcap_Dashboard_Report_Action
  • Wcap_Dashboard_Widget
  • WCAP_Dashboard_Widget_Heartbeat
  • Wcap_Dashboard_Widget_Report
  • WCAP_DB_Layer
  • Wcap_Default_Settings
  • Wcap_Display_Notices
  • Wcap_EDD
  • Wcap_Eent_Email_List
  • Wcap_Email_Settings
  • Wcap_Email_Template_Fields
  • Wcap_Email_Template_List
  • Wcap_Import_Lite_to_Pro
  • Wcap_Load_Hooks
  • Wcap_Load_Scripts
  • Wcap_Localization
  • WCAP_Manual_Email
  • Wcap_Menu
  • WCAP_On_Placed_Order
  • Wcap_Order_Received
  • Wcap_Personal_Data_Eraser
  • Wcap_Personal_Data_Export
  • Wcap_Populate_Cart_Of_User
  • Wcap_Print_And_CSV
  • Wcap_Product_Report_List
  • Wcap_Product_Report_Table
  • Wcap_Recover_Orders_Table
  • Wcap_Recover_Trash_Orders_Table
  • Wcap_Recovered_Order_List
  • Wcap_Send_Manual_Email
  • Wcap_Sent_Emails_Table
  • Wcap_SMS
  • Wcap_SMS_Templates
  • Wcap_Templates_Table
  • Wcap_Tiny_Mce
  • WCAP_Tiny_Url
  • Wcap_Tracking_msg
  • Wcap_TS_deactivate
  • Wcap_TS_Faq_Support
  • Wcap_TS_Tracker
  • Wcap_TS_tracking
  • Wcap_TS_Welcome
  • Wcap_TS_Woo_Active
  • Wcap_Update_Check
  • Woocommerce_Abandon_Cart

Functions

  • wcap_add_notification_meta
  • wcap_get_notification_meta
  • wcap_get_notification_meta_by_key
  • wcap_get_template_status
  • wcap_update_notification_meta
  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 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 
<?php
/**
 * Abandoned Cart Pro for WooCommerce
 *
 * It will send custom email to selected abandoned cart's users. The admin of the store will be able to send the abandoned cart reminder to specific abandoned cart(s). Also, the admin can edit the existing email template for sending the email.
 * 
 * @author   Tyche Softwares
 * @package  Abandoned-Cart-Pro-for-WooCommerce/Classes
 * @category Classes
 * @since    5.0
 */

// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * This class is used to start Initiate Recovery. 
 * 
 * @since 4.2
 */
class Wcap_Send_Manual_Email {

    /**
     * It will replace the merge tags and send the email to customer(s).
     * @global mixed $wpdb
     * @global mixed $woocommerce 
     */
    public static function wcap_create_and_send_manual_email (){

        global $wpdb;
        global $woocommerce;

        $abandoned_cart_ids   = explode ( ",", $_POST ['abandoned_cart_id'] );
        $selected_template_id = $_POST ['wcap_manual_template_name'];
        $get_template_data            = "SELECT * FROM " . WCAP_EMAIL_TEMPLATE_TABLE . " WHERE id =". $selected_template_id;
        $results_of_get_template_data = $wpdb->get_results ( $get_template_data );
        $default_template             =  $results_of_get_template_data[0]->default_template;
        $discount_amount              =  $results_of_get_template_data[0]->discount;
        $wcap_current_time            = current_time( 'timestamp' );

        foreach ( $abandoned_cart_ids as $abandoned_cart_ids_key => $abandoned_cart_ids_value ){

            $email_body_template    = isset( $_POST['woocommerce_ac_email_body'] ) ? $_POST['woocommerce_ac_email_body'] : '';
            $email_body_template    = stripslashes( $email_body_template );
            $email_body_template    = convert_smilies( $email_body_template );
            $template_email_subject = isset( $_POST['woocommerce_ac_email_subject'] ) ? $_POST['woocommerce_ac_email_subject'] : '';
            $template_email_subject = convert_smilies( $template_email_subject );
            $wcap_from_name         = get_option ( 'wcap_from_name' );
            $wcap_from_email        = get_option ( 'wcap_from_email' );
            $wcap_reply_email       = get_option ( 'wcap_reply_email' );

            $headers                = "From: " . $wcap_from_name . " <" . $wcap_from_email . ">" . "\r\n";
            $headers               .= "Content-Type: text/html"."\r\n";
            $headers               .= "Reply-To:  " . $wcap_reply_email . " " . "\r\n";

            $coupon_id              = isset( $_POST['coupon_ids'] ) ? $_POST['coupon_ids'][0] : '' ;
            $coupon_to_apply        = get_post( $coupon_id, ARRAY_A );
            $coupon_code            = $coupon_to_apply['post_title'];

            $generate_unique_code   = isset( $_POST['unique_coupon'] ) ? $_POST['unique_coupon'] : '' ;
            $is_wc_template         = isset( $_POST['is_wc_template'] ) ? $_POST['is_wc_template'] : '' ;
            $wc_template_header_t   = $_POST['wcap_wc_email_header'] != '' ? $_POST['wcap_wc_email_header'] : __( 'Abandoned cart reminder', 'woocommerce-ac');
            $coupon_code_to_apply   = $email_subject = '';

            $get_abandoned_cart        = "SELECT * FROM " . WCAP_ABANDONED_CART_HISTORY_TABLE . " WHERE id =". $abandoned_cart_ids_value;
            $results_of_abandoned_cart = $wpdb->get_results ( $get_abandoned_cart );

            $value = new stdClass();
            $value->user_type           = isset( $results_of_abandoned_cart[0]->user_type ) ? $results_of_abandoned_cart[0]->user_type : '';
            $value->user_id             = isset( $results_of_abandoned_cart[0]->user_id ) ? $results_of_abandoned_cart[0]->user_id : '';
            $value->abandoned_cart_info = isset( $results_of_abandoned_cart[0]->abandoned_cart_info ) ? $results_of_abandoned_cart[0]->abandoned_cart_info : '';
            $value->abandoned_cart_time = isset( $results_of_abandoned_cart[0]->abandoned_cart_time ) ? $results_of_abandoned_cart[0]->abandoned_cart_time : '';
            $value->language            = isset( $results_of_abandoned_cart[0]->language ) ? $results_of_abandoned_cart[0]->language : '';
            $wcap_guest_session_id      = isset( $results_of_abandoned_cart[0]->session_id ) ? $results_of_abandoned_cart[0]->session_id : 0;;
            $value->ac_id               = isset( $results_of_abandoned_cart[0]->id ) ? $results_of_abandoned_cart[0]->id : '';
            $wcap_used_coupon     = '';

            $selected_lanaguage = '';
            if ( $value->user_type == "GUEST" && $value->user_id != '0' ) {
                $value->user_login  = "";
                $query_guest        = "SELECT billing_first_name, billing_last_name, email_id FROM `" . WCAP_GUEST_CART_HISTORY_TABLE . "` WHERE id = %d";
                $results_guest      = $wpdb->get_results( $wpdb->prepare( $query_guest, $value->user_id ) );
                if (count ($results_guest) > 0 ){
                    $value->user_email  = $results_guest[0]->email_id;
                }

                $query_guest_session   = "SELECT session_value FROM `" . $wpdb->prefix . "woocommerce_sessions` WHERE session_key = %s";
                $results_guest_session = $wpdb->get_results( $wpdb->prepare( $query_guest_session, $wcap_guest_session_id  ) );

                if ( count( $results_guest_session ) > 0 ){
                    $wcap_result_session  = unserialize ( $results_guest_session[0]->session_value );
                    $wcap_coupon_sesson   = unserialize ( $wcap_result_session['applied_coupons'] );
                    $wcap_used_coupon     = '';
                    $coupon_code_to_apply = '';
                    if ( count( $wcap_coupon_sesson ) > 0 && isset( $wcap_coupon_sesson[0]) ) {
                        $wcap_used_coupon       = $wcap_coupon_sesson[0];
                        $coupon_code_to_apply   = $wcap_used_coupon;
                    }
                }

            } else {
                $user_id            = $value->user_id;
                $user_email_biiling = get_user_meta( $user_id, 'billing_email', true );

                if ( isset( $user_email_biiling ) && "" == $user_email_biiling ) {
                    $user_data = get_userdata( $user_id );
                    
                    if ( isset( $user_data->user_email ) && "" != $user_data->user_email ) {
                        $value->user_email = $user_data->user_email;
                    } 
                } else if ( '' != $user_email_biiling ) {
                    $value->user_email = $user_email_biiling;
                }

                $logged_in_user_key = get_post_meta ( $value->ac_id , '_woocommerce_ac_coupon');
                if ( count( $logged_in_user_key ) > 0 ){

                    $wcap_used_coupon =  $logged_in_user_key[0]['coupon_code'];
                    $coupon_code_to_apply = $wcap_used_coupon;
                }
            }
            $cart = new stdClass();
            $cart_info_db_field = json_decode( stripslashes( $value->abandoned_cart_info ) );
            if( !empty( $cart_info_db_field->cart ) ) {
                $cart           = $cart_info_db_field->cart;
            }

            // Currency selected
            $currency = isset( $cart_info_db_field->currency ) ? $cart_info_db_field->currency : '';

            $validate_email_format = Wcap_Send_Manual_Email::wcap_validate_email_format ( $value->user_email );

            if( count( get_object_vars( $cart ) ) > 0 && $value->user_id != '0' && $validate_email_format === 1 ) {
                $cart_update_time   = $value->abandoned_cart_time;
                $selected_lanaguage = $value->language;
                $wc_template_header = $wc_template_header_t;
                $cart_info_db       = $value->abandoned_cart_info;
                $email_body         = $email_body_template;
                $email_body         .= '{{email_open_tracker}}';

                if ( $value->user_type == "GUEST" ) {
                    if ( isset( $results_guest[0]->billing_first_name ) ) {
                        $email_body    = str_replace( "{{customer.firstname}}", $results_guest[0]->billing_first_name, $email_body );
                        $email_subject = str_replace( "{{customer.firstname}}", $results_guest[0]->billing_first_name, $template_email_subject );
                    }

                    if ( isset( $results_guest[0]->billing_last_name ) ) {
                        $email_body = str_replace( "{{customer.lastname}}", $results_guest[0]->billing_last_name, $email_body );
                    }

                    if ( isset( $results_guest[0]->billing_first_name ) && isset( $results_guest[0]->billing_last_name ) ) {
                        $email_body = str_replace( "{{customer.fullname}}", $results_guest[0]->billing_first_name . " " . $results_guest[0]->billing_last_name, $email_body );
                    }
                    else if ( isset( $results_guest[0]->billing_first_name ) ){
                        $email_body = str_replace( "{{customer.fullname}}", $results_guest[0]->billing_first_name, $email_body );
                    }
                    else if ( isset( $results_guest[0]->billing_last_name) ){
                        $email_body = str_replace( "{{customer.fullname}}", $results_guest[0]->billing_last_name, $email_body );
                    }
                } else {
                    $user_first_name_temp = get_user_meta( $value->user_id, 'billing_first_name', true );
                    if( isset( $user_first_name_temp ) && "" == $user_first_name_temp ) {
                        $user_data  = get_userdata( $user_id );
                        $user_first_name = $user_data->first_name;
                    } else {
                        $user_first_name = $user_first_name_temp;
                    }

                    $user_last_name_temp = get_user_meta( $value->user_id, 'billing_last_name', true );
                    if( isset( $user_last_name_temp ) && "" == $user_last_name_temp ) {
                        $user_data  = get_userdata( $user_id );
                        $user_last_name = $user_data->last_name;
                    } else {
                        $user_last_name = $user_last_name_temp;
                    }
                    $email_body    = str_replace( "{{customer.firstname}}", $user_first_name, $email_body );
                    $email_subject = str_replace( "{{customer.firstname}}", $user_first_name, $template_email_subject );
                    $email_body    = str_replace( "{{customer.lastname}}", $user_last_name, $email_body );
                    $email_body    = str_replace( "{{customer.fullname}}", $user_first_name . " " . $user_last_name, $email_body );
                }

                $wcap_get_customers_email = Wcap_Send_Manual_Email::wcap_get_customers_email( $value->user_id, $value->user_type );
                $email_body = str_replace( "{{customer.email}}", $wcap_get_customers_email, $email_body );

                $wcap_get_customers_phone = Wcap_Send_Manual_Email::wcap_get_customers_phone( $value->user_id, $value->user_type );
                $email_body = str_replace( "{{customer.phone}}", $wcap_get_customers_phone, $email_body );
                $order_date = "";

                if ( $cart_update_time != "" && $cart_update_time != 0 ) {
                    $date_format   = date_i18n( get_option( 'date_format' ), $cart_update_time );
                    $time_format   = date_i18n( get_option( 'time_format' ), $cart_update_time );
                    $order_date    = $date_format . ' ' . $time_format;
                }

                $expiry_date_extend               = date( "Y-m-d", strtotime( date( 'Y-m-d' ) . " +7 days" ) );
                if( preg_match( "{{coupon.code}}", $email_body, $matched ) ) {
                    $coupon_post_meta = '';
                    if( '1' == $default_template && $coupon_code == '' ) {
                        if( '5' == $discount_amount ) {
                            $amount               = $discount_amount; // Amount
                            $discount_type        = 'percent';
                            $expiry_date          = apply_filters( 'wcap_coupon_expiry_date', $expiry_date_extend );
                            $coupon_code_to_apply = Wcap_Send_Manual_Email::wp_coupon_code ( $amount, $discount_type, $expiry_date, $coupon_post_meta );
                        } elseif ( '10' == $discount_amount ) {
                            $amount               = $discount_amount; // Amount
                            $discount_type        = 'percent';
                            $expiry_date          = apply_filters( 'wcap_coupon_expiry_date', $expiry_date_extend );
                            $coupon_code_to_apply = Wcap_Send_Manual_Email::wp_coupon_code ( $amount, $discount_type, $expiry_date, $coupon_post_meta );
                        }
                    } elseif( $coupon_code != '' && $generate_unique_code == 'on' ) {
                        $coupon_post_meta         = get_post_meta( $coupon_id );
                        $get_current_date         = date( get_option( 'date_format' ) );                       
                        $coupon_expiry_timestamp  = strtotime( $coupon_post_meta['expiry_date'][0] );                       
                        $current_date_timestamp   = strtotime( $get_current_date ); 
                        $discount_type            = $coupon_post_meta['discount_type'][0];
                        $amount                   = $coupon_post_meta['coupon_amount'][0];
                        if( isset( $coupon_post_meta['expiry_date'][0] ) && $coupon_expiry_timestamp < $current_date_timestamp && $coupon_post_meta['expiry_date'][0] != '' ) {
                            $expiry_date = $coupon_post_meta['expiry_date'][0];
                        } else {
                            $expiry_date = apply_filters( 'wcap_coupon_expiry_date', $expiry_date_extend );
                        }

                        if( isset( $coupon_post_meta['expiry_date'][0] ) && $coupon_expiry_timestamp >= $wcap_current_time && $coupon_post_meta['expiry_date'][0] != '' ) {
                            $expiry_date = $coupon_post_meta['expiry_date'][0];
                        }else{
                            $expiry_date = apply_filters( 'wcap_coupon_expiry_date', $expiry_date_extend );
                        }
                        $coupon_code_to_apply = Wcap_Send_Manual_Email::wp_coupon_code( $amount, $discount_type, $expiry_date, $coupon_post_meta );
                    } else {
                        $coupon_code_to_apply = $coupon_code;
                    }
                    $email_body = str_replace( "{{coupon.code}}", $coupon_code_to_apply, $email_body );
                }

                $email_body  = str_replace( "{{cart.abandoned_date}}", $order_date, $email_body );
                $email_body  = str_replace( "{{shop.name}}", get_option( 'blogname' ), $email_body );
                $email_body  = str_replace( "{{shop.url}}", get_option( 'siteurl' ), $email_body );
                $uid         = get_current_user_id();
                $admin_phone = get_user_meta( $uid, 'billing_phone', true );
                $email_body  = str_replace( '{{admin.phone}}', $admin_phone, $email_body );

                if ( $woocommerce->version < '2.3' ) {
                    $checkout_page_link = $woocommerce->cart->get_checkout_url();
                } else {
                    $checkout_page_id   = wc_get_page_id( 'checkout' );
                    $checkout_page_link = '';
                    if( $checkout_page_id ) {
                        // Get the checkout URL
                        $checkout_page_link = get_permalink( $checkout_page_id );

                        if( function_exists( 'icl_register_string' ) ) {
                            if( 'en' == $selected_lanaguage  ) {
                                $checkout_page_link = $checkout_page_link;
                            } else {
                                $checkout_page_link = apply_filters( 'wpml_permalink', $checkout_page_link, $selected_lanaguage );
                            }
                        }
                        // Force SSL if needed
                        if ( is_ssl() || 'yes' === get_option( 'woocommerce_force_ssl_checkout' ) ) {
                            $checkout_page_link = str_replace( 'http:', 'https:', $checkout_page_link );
                        }
                    }
                }

                $query_sent = "INSERT INTO `" . WCAP_EMAIL_SENT_HISTORY_TABLE . "` ( template_id, abandoned_order_id, sent_time, sent_email_id )
                                      VALUES ( '" . $selected_template_id . "', '" . $abandoned_cart_ids_value . "', '" . current_time('mysql') . "', '" . $value->user_email . "' )";
                $wpdb->query( $query_sent );
                $query_id          = "SELECT * FROM `" . WCAP_EMAIL_SENT_HISTORY_TABLE . "` WHERE template_id= %d AND abandoned_order_id= %d ORDER BY id DESC LIMIT 1 ";
                $results_sent      = $wpdb->get_results ( $wpdb->prepare( $query_id, $selected_template_id, $abandoned_cart_ids_value ) );
                $email_sent_id     = $results_sent[0]->id;

                $encoding_checkout = $email_sent_id . '&url=' . $checkout_page_link;
                $validate_checkout = Wcap_Common::encrypt_validate( $encoding_checkout );

                if( isset( $coupon_code_to_apply ) && $coupon_code_to_apply != '' ) {
                    $encypted_coupon_code = Wcap_Common::encrypt_validate( $coupon_code_to_apply );
                    $checkout_link_track  = get_option( 'siteurl' ) . '/?wacp_action=track_links&validate=' . $validate_checkout . '&c='.$encypted_coupon_code;
                } else {
                    $checkout_link_track = get_option( 'siteurl' ) . '/?wacp_action=track_links&validate=' . $validate_checkout;
                }

                // Populate the product name if its present in the email subject line
                $sub_line_prod_name = '';
                $cart_details       = $cart_info_db_field->cart;
                foreach ( $cart_details as $k => $v ) {
                    $sub_line_prod_name = get_the_title( $v->product_id );
                    break;
                }
                $email_subject = str_replace( "{{product.name}}", $sub_line_prod_name, $email_subject );
                // Populate the products.cart shortcode if it exists
                $wcap_product_image_height = get_option( 'wcap_product_image_height' );
                $wcap_product_image_width  = get_option( 'wcap_product_image_width' );

                if ( preg_match( "{{item.image}}", $email_body, $matched ) || preg_match( "{{item.name}}", $email_body, $matched ) || preg_match( "{{item.price}}", $email_body, $matched ) || preg_match( "{{item.quantity}}", $email_body, $matched ) || preg_match( "{{item.subtotal}}", $email_body, $matched ) || preg_match( "{{cart.total}}", $email_body, $matched ) ) {
                    $replace_html   = '';
                    $cart_details   = $cart_info_db_field->cart;
                    $cart_total     = $item_subtotal = $item_total =  $line_subtotal_tax_display = $after_item_subtotal = $after_item_subtotal_display = 0;
                    $line_subtotal_tax = 0;

                    $wcap_all_product_names = '';
                    $wcap_all_product_images = '';
                    $wcap_all_product_price = '';
                    $wcap_all_product_qty = '';
                    $wcap_all_product_sub_total = '';

                    $wcap_include_tax         = get_option( 'woocommerce_prices_include_tax' );
                    $wcap_include_tax_setting = get_option( 'woocommerce_calc_taxes' );
                    // This array will be used to house the columns in the hierarchy they appear
                    $position_array = array();
                    $start_position = $end_position = $image_start_position = $name_start_position = 0;
                    

                    // Complete populating the array
                    ksort( $position_array );

                    $tr_array   = explode( "<tr", $email_body );
                    $check_html = $style = '';
                    foreach ( $tr_array as $tr_key => $tr_value ) {
                        if( (preg_match( "{{item.image}}", $tr_value, $matched ) || preg_match( "{{item.name}}", $tr_value, $matched) || preg_match( "{{item.price}}", $tr_value, $matched ) || preg_match( "{{item.quantity}}", $tr_value, $matched) || preg_match( "{{item.subtotal}}", $tr_value, $matched)) && ! preg_match( "{{cart.total}}", $tr_value, $matched ) ) {

                            if ( count( get_object_vars( $cart_details ) ) > 1 ) {
                                $style_start  = strpos( $tr_value, 'style' );
                                $style_end    = strpos( $tr_value, '>', $style_start );
                                $style_end    = $style_end - $style_start;
                                $style        = substr( $tr_value, $style_start, $style_end );
                                $tr_value     = "<tr" . $tr_value;
                                $end_position = strpos( $tr_value, '</tr>' );
                                $end_position = $end_position + 5;
                                $check_html   = substr( $tr_value, 0, $end_position );
                            }

                            //check which columns are present
                            if ( preg_match( "{{item.image}}", $email_body, $matched ) ) {
                                $image_start_position = strpos( $email_body, '{{item.image}}' );
                                $position_array[ $image_start_position ] = 'image';
                            }
                            if ( preg_match( "{{item.name}}", $email_body, $matched ) ) {
                                $name_start_position = strpos( $email_body,'{{item.name}}' );
                                $position_array[ $name_start_position ] = 'name';
                            }
                            if ( preg_match( "{{item.price}}", $email_body, $matched ) ) {
                                $price_start_position = strpos( $email_body, '{{item.price}}' );
                                $position_array[ $price_start_position ] = 'price';
                            }
                            if ( preg_match( "{{item.quantity}}", $email_body, $matched ) ) {
                                $quantity_start_position = strpos( $email_body, '{{item.quantity}}' );
                                $position_array[ $quantity_start_position ] = 'quantity';
                            }
                            if ( preg_match( "{{item.subtotal}}", $email_body, $matched ) ) {
                                $subtotal_start_position = strpos( $email_body,'{{item.subtotal}}' );
                                $position_array[ $subtotal_start_position ] = 'subtotal';
                            }
                        }
                    }

                    $i = 1;
                    $image_url = '';
                    $display_cart_total = 0;
                    $bundle_child = array();
                    foreach ( $cart_details as $k => $v ) {
                        // Product image
                        if( version_compare( $woocommerce->version, '3.0.0', ">=" ) ) {
                          $wcap_product   = wc_get_product($v->product_id );
                          $product        = wc_get_product($v->product_id );
                        }else {
                            $product      = get_product( $v->product_id );
                            $wcap_product = get_product($v->product_id );
                        }
                            
                        if ( false !== $product ) {
                            $image_size   = array( $wcap_product_image_width, $wcap_product_image_height, '1' );
                            $image_url    = Wcap_Common::wcap_get_product_image( $v->product_id, $image_size );

                            // Populate the name variable
                            $product_name = get_post( $v->product_id );
                            $item_name    = $product_name->post_title;
                            $prod_name    = apply_filters( 'wcap_product_name', $item_name );                                                      
                            // Populate qty
                            $quantity  = $v->quantity;

                           
                            if( version_compare( $woocommerce->version, '3.0.0', ">=" ) ) {
                                $wcap_product_type = $wcap_product->get_type();
                            }else {
                                $wcap_product_type = $wcap_product->product_type;
                            }
                            $wcap_product_sku = apply_filters( 'wcap_product_sku', $product->get_sku() );
                            if( false != $wcap_product_sku && '' != $wcap_product_sku ) {
                                if( $wcap_product_type == 'simple' && '' != $wcap_product->get_sku() ){
                                    $wcap_sku = '<br> SKU: ' . $wcap_product->get_sku();        
                                } else {
                                    $wcap_sku = '';    
                                }
                                $prod_name    = $prod_name . $wcap_sku;
                            } else {
                                $prod_name    = $prod_name;
                            }    
                            // Show variation
                            if( isset( $v->variation_id ) && '' != $v->variation_id ){
                                $variation_id = $v->variation_id;
                                $variation    = wc_get_product( $variation_id );

                                if ( false !== $variation ) {
                                    $name         = $variation->get_formatted_name() ;
                                    $explode_all  = explode( "&ndash;", $name );
                                    if( version_compare( $woocommerce->version, '3.0.0', ">=" ) ) {
                                        if( false != $wcap_product_sku && '' != $wcap_product_sku ) {
                                            $wcap_sku = '';
                                            if ( $variation->get_sku() ) {
                                                $wcap_sku = "SKU: " . $variation->get_sku() . "<br>";
                                            }
                                            $wcap_get_formatted_variation  =  wc_get_formatted_variation( $variation, true );

                                            $add_product_name = $prod_name . ' - ' . $wcap_sku . ' ' .$wcap_get_formatted_variation;
                                        } else {
                                            $wcap_get_formatted_variation  =  wc_get_formatted_variation( $variation, true );
                                            $add_product_name = $prod_name . '<br>' . $wcap_get_formatted_variation;
                                        }                                          
                                        $pro_name_variation = (array) $add_product_name;
                                    } else {
                                        $pro_name_variation = array_slice( $explode_all, 1, -1 );
                                    }
                                    $product_name_with_variable = '';
                                    $explode_many_varaition     = array();

                                    foreach ( $pro_name_variation as $pro_name_variation_key => $pro_name_variation_value ){
                                        $explode_many_varaition = explode ( ",", $pro_name_variation_value );
                                        if( !empty( $explode_many_varaition ) ) {
                                            foreach( $explode_many_varaition as $explode_many_varaition_key => $explode_many_varaition_value ) {
                                                $product_name_with_variable = $product_name_with_variable .  html_entity_decode ( $explode_many_varaition_value ) . "<br>";
                                            }
                                        } else {
                                            $product_name_with_variable = $product_name_with_variable .  html_entity_decode ( $explode_many_varaition_value ) . "<br>";
                                        }
                                    }
                                    $prod_name = $product_name_with_variable;
                                }
                            }
                            if( isset( $wcap_include_tax ) && $wcap_include_tax == 'no' &&
                            isset( $wcap_include_tax_setting ) && $wcap_include_tax_setting == 'yes' ) {
                                $item_subtotal       =  $v->line_subtotal;
                                $after_item_subtotal =  $v->line_total ;
                                $line_subtotal_tax   += $v->line_tax;
                                $display_cart_total  = $v->line_total +  $v->line_tax + $display_cart_total;
                            } elseif ( isset( $wcap_include_tax ) && $wcap_include_tax == 'yes' &&
                            isset( $wcap_include_tax_setting ) && $wcap_include_tax_setting == 'yes' ) {
                                // Item subtotal is calculated as product total including taxes
                                if( $v->line_tax != 0 && $v->line_tax > 0 ) {

                                    $line_subtotal_tax_display += $v->line_tax;

                                    // After copon code price
                                    $after_item_subtotal = $item_subtotal + $v->line_total + $v->line_tax;

                                    // Calculate the product price
                                    $item_subtotal = $item_subtotal + $v->line_subtotal + $v->line_subtotal_tax;
                                } else {
                                    $item_subtotal = $item_subtotal + $v->line_total;
                                    $line_subtotal_tax_display += $v->line_tax;
                                    $after_item_subtotal = $item_subtotal + $v->line_tax;
                                }
                            } else {

                                if( $v->line_subtotal_tax != 0 && $v->line_subtotal_tax > 0 ) {
                                    $after_item_subtotal = $v->line_total + $v->line_subtotal_tax;
                                    $item_subtotal = $item_subtotal + $v->line_subtotal + $v->line_subtotal_tax;
                                } else {
                                    $after_item_subtotal = $v->line_total;
                                    $item_subtotal = $item_subtotal + $v->line_total;
                                }
                            }

                            //  Line total
                            $item_total                  = $item_subtotal;
                            $item_price                  = $item_subtotal / $quantity;
                            $after_item_subtotal_display = ( $item_subtotal - $after_item_subtotal ) +  $after_item_subtotal_display ;
                            $item_subtotal_display       = apply_filters ( 'acfac_change_currency', Wcap_Common::wcap_get_price( $item_total, $currency ), $abandoned_cart_ids_value, $item_total, 'wcap_manual_mail' ); 
                            $item_price                  =  apply_filters ( 'acfac_change_currency', Wcap_Common::wcap_get_price( $item_price, $currency ), $abandoned_cart_ids_value, $item_price, 'wcap_manual_mail' ); 
                            $cart_total                 += $after_item_subtotal;
                            $item_subtotal               = $item_total = 0;

                            /*if( $i % 2 == 0 ) {
                                $replace_html .= '<tr>';
                            } else {*/
                                $replace_html .= '<tr ' . $style . '>';
                            //}

                            // If bundled product, get the list of sub products
                            if( isset( $v->product_type ) && 'bundle' == $v->product_type && isset( $product->bundle_data ) && is_array( $product->bundle_data ) && count( $product->bundle_data ) > 0 ) {
                                foreach ( $product->bundle_data as $b_key => $b_value ) {
                                    $bundle_child[] = $b_key;
                                }
                            }
                            // Check if the product is a part of the bundles product, if yes, set qty and totals to blanks
                            if( isset( $bundle_child ) && count( $bundle_child ) > 0 ) {
                                if ( in_array( $v->product_id, $bundle_child ) ) {
                                    $item_subtotal_display = $item_price = $quantity = '';
                                }
                            }

                            $wcap_all_product_names = $prod_name . ", " . $wcap_all_product_names ;

                            $wcap_all_product_images = $image_url . ", " . $wcap_all_product_images ;

                            $wcap_all_product_price  = $item_price . ", " . $wcap_all_product_price ;
                            
                            $wcap_all_product_qty    = $quantity . ", " . $wcap_all_product_qty ;

                            $wcap_all_product_sub_total    = $item_subtotal_display . ", " . $wcap_all_product_sub_total ;

                            
                            
                            foreach( $position_array as $k => $v ) {
                                switch( $v ) {
                                    case 'image':
                                        $replace_html .= '<td style="text-align:center;"> <a href="' . $checkout_link_track . '">' . $image_url . '</a> </td>';
                                        break;
                                    case 'name':
                                        $replace_html .= '<td style="text-align:center;"> <a href="' . $checkout_link_track . '">' . $prod_name . '</a> </td>';
                                        break;
                                    case 'price':
                                        if ( $item_price == '' ) {
                                            $replace_html .= '<td></td>';
                                        }
                                        else {
                                            $replace_html .= '<td style="text-align:center;">' . $item_price . '</td>';
                                        }
                                        break;
                                    case 'quantity':
                                        $replace_html .= '<td style="text-align:center;">' . $quantity . '</td>';
                                        break;
                                    case 'subtotal':
                                        if ( $item_subtotal_display == '' ) {
                                            $replace_html .= '<td></td>';
                                        }
                                        else {
                                            $replace_html .= '<td style="text-align:center;">' . $item_subtotal_display . '</td>';
                                        }
                                        break;
                                    default:
                                        $replace_html .= '<td></td>';
                                }
                            }
                            $replace_html .= '</tr>';
                        } else {
                            $replace_html .= '<tr > <td colspan="5"> Product you had added to cart is currently unavailable. Please choose another product from <a href="'.get_option( 'siteurl' ).'">'.get_option( 'blogname' ).'</a> </td> </tr>';
                            $after_item_subtotal_display = $wcap_line_subtotal_tax = '';
                        }

                        $i++;
                    }

                    if( isset($after_item_subtotal_display) && $after_item_subtotal_display > 0 ) {
                        $after_item_subtotal_display = apply_filters ( 'acfac_change_currency', Wcap_Common::wcap_get_price( $after_item_subtotal_display, $currency ), $abandoned_cart_ids_value, $after_item_subtotal_display, 'wcap_manual_mail' ); 
                        $replace_html .= '<tr>
                                        <td> </td>
                                        <td> </td>
                                        <td> </td>
                                        <td>'.__( "<strong>Coupon: $wcap_used_coupon</strong>", "woocommerce-ac" ).'</td>
                                        <td> -'. $after_item_subtotal_display .'</td>
                                    </tr>';
                    }

                    if( isset($wcap_include_tax) && $wcap_include_tax == 'no' &&
                        isset($wcap_include_tax_setting) && $wcap_include_tax_setting == 'yes' ) {
                        $line_subtotal_tax =  apply_filters ( 'acfac_change_currency', Wcap_Common::wcap_get_price( $line_subtotal_tax, $currency ), $abandoned_cart_ids_value, $line_subtotal_tax, 'wcap_manual_mail' );

                        $replace_html .= '<tr>
                                            <td> </td>
                                            <td> </td>
                                            <td> </td>
                                            <td>' . __( "<strong>Tax:</strong>", "woocommerce-ac" ) . '</td>
                                            <td>' . $line_subtotal_tax . '</td>
                                        </tr>';
                    }
                    // Calculate the cart total
                     if( isset( $wcap_include_tax ) && $wcap_include_tax == 'yes' &&
                        isset( $wcap_include_tax_setting ) && 'yes' == $wcap_include_tax_setting ) {
                        $cart_total = apply_filters ( 'acfac_change_currency', Wcap_Common::wcap_get_price( $cart_total, $currency ), $abandoned_cart_ids_value, $cart_total, 'wcap_manual_mail' );
                        $line_subtotal_tax_display = apply_filters ( 'acfac_change_currency', Wcap_Common::wcap_get_price( $line_subtotal_tax_display, $currency ), $abandoned_cart_ids_value, $line_subtotal_tax_display, 'wcap_manual_mail' ); 

                        $cart_total =  $cart_total . ' ( ' . __( "includes" , "woocommerce-ac" ) . $line_subtotal_tax_display . __( "Tax:", "woocommerce-ac" ) . ')';
                    } elseif( isset( $wcap_include_tax ) && $wcap_include_tax == 'no' &&
                        isset( $wcap_include_tax_setting ) && 'yes' == $wcap_include_tax_setting ) {

                        $display_cart_total = apply_filters ( 'acfac_change_currency', Wcap_Common::wcap_get_price( $display_cart_total, $currency ), $abandoned_cart_ids_value, $display_cart_total, 'wcap_manual_mail' );
                        $cart_total = $display_cart_total;
                    } else {

                        $cart_total = apply_filters ( 'acfac_change_currency', Wcap_Common::wcap_get_price( $cart_total, $currency ), $abandoned_cart_ids_value, $cart_total, 'wcap_manual_mail' );
                    }

                    $wcap_all_product_names = substr( $wcap_all_product_names, 0, -2 );

                    $wcap_all_product_images = substr( $wcap_all_product_images, 0, -2 );

                    $wcap_all_product_price = substr( $wcap_all_product_price, 0, -2 );

                    $wcap_all_product_qty = substr( $wcap_all_product_qty, 0, -2 );

                    $wcap_all_product_sub_total = substr( $wcap_all_product_sub_total, 0, -2 );

                    
                    // Populate/Add the product rows
                    $email_body     = str_replace( $check_html, $replace_html, $email_body );
                    // Populate the cart total
                    $email_body     = str_replace( "{{cart.total}}", $cart_total, $email_body );
                    $email_body     = str_replace( "{{item.name}}", $wcap_all_product_names, $email_body );
                    $replace_image  = $image_url;
                    $email_body     = str_replace( "{{item.image}}", $wcap_all_product_images, $email_body );
                    $email_body     = str_replace( "{{item.price}}", $wcap_all_product_price, $email_body );
                    $email_body     = str_replace( "{{item.quantity}}", $wcap_all_product_qty, $email_body );
                    $email_body     = str_replace( "{{item.subtotal}}", $wcap_all_product_sub_total, $email_body );
                }
                if( $woocommerce->version < '2.3' ) {
                    $cart_page_link = $woocommerce->cart->get_cart_url();
                } else {
                    $cart_page_id   = wc_get_page_id( 'cart' );
                    $cart_page_link = $cart_page_id ? get_permalink( $cart_page_id ) : '';
                }

                if( function_exists( 'icl_register_string' ) ) {
                    if( 'en' == $selected_lanaguage ) {
                        $cart_page_link = $cart_page_link;
                    } else {
                        $cart_page_link = apply_filters( 'wpml_permalink', $cart_page_link, $selected_lanaguage );
                    }
                }
                $email_body    = str_replace( "{{checkout.link}}", $checkout_link_track, $email_body );
                $encoding_cart = $email_sent_id . '&url=' . $cart_page_link ;
                $validate_cart = Wcap_Common::encrypt_validate( $encoding_cart );

                if( isset( $coupon_code_to_apply ) && $coupon_code_to_apply != '' ) {
                    $encypted_coupon_code = Wcap_Common::encrypt_validate( $coupon_code_to_apply );
                    $cart_link_track = get_option( 'siteurl' ) . '/?wacp_action=track_links&validate=' . $validate_cart . '&c=' . $encypted_coupon_code;
                } else {
                    $cart_link_track = get_option( 'siteurl' ) . '/?wacp_action=track_links&validate=' . $validate_cart;
                }
                $email_body                    = str_replace( "{{cart.link}}", $cart_link_track, $email_body );
                $validate_unsubscribe          = Wcap_Common::encrypt_validate( $email_sent_id );
                $email_sent_id_address         = $results_sent[0]->sent_email_id;;
                $encrypt_email_sent_id_address = hash( 'sha256', $email_sent_id_address );
                $plugins_url                   = get_option( 'siteurl' ) . "/?wcap_track_unsubscribe=wcap_unsubscribe&validate=" . $validate_unsubscribe . "&track_email_id=" . $encrypt_email_sent_id_address;
                $unsubscribe_link_track        = $plugins_url;

                $email_body                    = str_replace( "{{cart.unsubscribe}}" , $unsubscribe_link_track , $email_body );
                $plugins_url_track_image       = get_option( 'siteurl' ) . '/?wcap_track_email_opens=wcap_email_open&email_id=';
                $hidden_image                  = '<img style="border:0px; height: 1px; width:1px;" alt="" src="' . $plugins_url_track_image . $email_sent_id . '" >';
                $email_body                    = str_replace( "{{email_open_tracker}}" , $hidden_image , $email_body );
                $user_email                    = $value->user_email;

                $email_body = str_replace ( "My document title", "", $email_body );

                if( isset( $is_wc_template ) && "on" == $is_wc_template ){

                    ob_start();

                    wc_get_template( 'emails/email-header.php', array( 'email_heading' => $wc_template_header ) );
                    $email_body_template_header = ob_get_clean();

                    ob_start();

                    wc_get_template( 'emails/email-footer.php' );
                    $email_body_template_footer = ob_get_clean();

                    $final_email_body =  $email_body_template_header . $email_body . $email_body_template_footer;

                    Wcap_Common::wcap_add_wc_mail_header();
                    wc_mail( $user_email, stripslashes( $email_subject ), stripslashes( $final_email_body ) , $headers );
                    Wcap_Common::wcap_remove_wc_mail_header();
                } else {
                    Wcap_Common::wcap_add_wp_mail_header();
                    wp_mail( $user_email, stripslashes( $email_subject ), stripslashes( $email_body ), $headers );
                    Wcap_Common::wcap_remove_wc_mail_header();
                }

                $update_manual_data = "UPDATE `" . WCAP_ABANDONED_CART_HISTORY_TABLE . "` SET manual_email = 'YES' WHERE id = '".$abandoned_cart_ids_value."' ";
                $wpdb->query( $update_manual_data );
            }
        }

        wp_safe_redirect( admin_url( '/admin.php?page=woocommerce_ac_page&action=listcart&wcap_manual_email_sent=YES' ) );
    }

    /**
     * This function will return the email address of the customer for the cart. As we have given the choice to the admin that he can choose who will receive the email template.
     *
     * @param int $wcap_user_id user ID
     * @param String $wcap_user_type User Type
     * @return String $wcap_email_address Email Address
     * @since 4.2
     */
    public static function wcap_get_customers_email( $wcap_user_id, $wcap_user_type ) {
        global $wpdb;

        $wcap_email_address = '';
        if( "GUEST" == $wcap_user_type && '0' != $wcap_user_id ) {
                
            $query_guest        = "SELECT billing_first_name, billing_last_name, email_id FROM `" . WCAP_GUEST_CART_HISTORY_TABLE . "` WHERE id = %d";
            $results_guest      = $wpdb->get_results( $wpdb->prepare( $query_guest, $wcap_user_id ) );
            if( count( $results_guest ) > 0 ) {
                $wcap_email_address = $results_guest[0]->email_id;
            }
        } else if( "GUEST" != $wcap_user_type && '0' != $wcap_user_id ) { 
            $key                = 'billing_email';
            $single             = true;
            $user_billing_email = get_user_meta( $wcap_user_id, $key, $single );
            if( isset( $user_billing_email ) && $user_billing_email != '' ) {
               $wcap_email_address = $user_billing_email;
           } else {
                $user_data  = get_userdata( $wcap_user_id );
                if ( isset( $user_data->user_email ) && '' != $user_data->user_email ) {
                    $wcap_email_address = $user_data->user_email;
                }
            }
        }
        return $wcap_email_address;
    }

    /**
     * This function will return the phone number of the customer for the cart.
     *
     * @param int $wcap_user_id user ID
     * @param String $wcap_user_type User Type
     * @return Int|String $wcap_customer_phone Phone number
     * @since  4.2
     */
    public static function wcap_get_customers_phone( $wcap_user_id, $wcap_user_type ) {
        global $wpdb;

        $wcap_customer_phone = '';
        if( "GUEST" == $wcap_user_type && '0' != $wcap_user_id ) {
                
            $get_phone_query_guest = "SELECT phone FROM `" . WCAP_GUEST_CART_HISTORY_TABLE . "` WHERE id = %d";
            $results_guest         = $wpdb->get_results( $wpdb->prepare( $get_phone_query_guest, $wcap_user_id ) );
            if( count( $results_guest ) > 0 ) {
                $wcap_customer_phone = $results_guest[0]->phone;
            }
        } else if( "GUEST" != $wcap_user_type && '0' != $wcap_user_id ) { 
            $user_phone_number = get_user_meta( $wcap_user_id, 'billing_phone' );
            if( isset( $user_phone_number[0] ) ) {
                $wcap_customer_phone = $user_phone_number[0];
            }
        }
        return $wcap_customer_phone;
    }

    /**
     * As we have given the choice to admin, that he can send email template to any one. So we need to check each email address is in correct format or not. If any one of the email address is correct then send the email. We are having the multiple email address in the comma seprated so we need to check validate email format.
     *
     * @param String $wcap_email_address Email Address of abandoned cart's user
     * @return String $validated_value Validate email address
     * @since  4.2
     */
    public static function wcap_validate_email_format( $wcap_email_address ) {

        if ( version_compare( phpversion(), '5.2.0', '>=') ) {
            $validated_value = filter_var( $wcap_email_address, FILTER_VALIDATE_EMAIL );

            if ( $validated_value == $wcap_email_address ){
                $validated_value = 1;
            }else{
                $validated_value = 0;
            }
        } else{
            $pattern = '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD';
            $validated_value = preg_match( $pattern, $wcap_email_address ) ;
        }
        return $validated_value;
    }
    /**
     * If the email tempalte will have the merge tag {{coupon.code}} and parent coupon code then our plugin will generate the coupon code. This function is used for getting the functionality of parent coupon code for the unique generated coupon code. Also, it will update coupon details in post_meta table.
     *
     * @param int $discount_amt coupon amount for default email template
     * @param string $get_discount_type Discount type
     * @param string $get_expiry_date Coupon expiry date
     * @param string $coupon_post_meta Retrieve post meta field
     * @return String $final_string Coupon code
     * @since 4.2
     */
    public static function wp_coupon_code( $discount_amt, $get_discount_type, $get_expiry_date, $coupon_post_meta ) {
        $ten_random_string = Wcap_Send_Manual_Email::wp_random_string();
        $first_two_digit   = rand( 0, 99 );
        $final_string      = $first_two_digit.$ten_random_string;
        $datetime          = $get_expiry_date ; //date( "Y-m-d", strtotime( date( 'Y-m-d' )." +7 days" ) );
        $coupon_code       = $final_string;

        $coupon_product_categories         = isset( $coupon_post_meta['product_categories'] [ 0 ] ) && $coupon_post_meta['product_categories'] [ 0 ] != '' ? unserialize( $coupon_post_meta['product_categories'] [ 0 ] )  : array();
        $coupon_exculde_product_categories = isset( $coupon_post_meta['exclude_product_categories'] [ 0 ] ) && $coupon_post_meta['exclude_product_categories'] [ 0 ] != '' ? unserialize ( $coupon_post_meta['exclude_product_categories'] [ 0 ] ) : array();
        $coupon_product_ids                = isset( $coupon_post_meta['product_ids'] [ 0 ] ) && $coupon_post_meta['product_ids'] [ 0 ] != '' ? $coupon_post_meta['product_ids'] [ 0 ] : '';
        $coupon_exclude_product_ids        = isset( $coupon_post_meta['exclude_product_ids'] [ 0 ] ) && $coupon_post_meta['exclude_product_ids'] [ 0 ] != '' ? $coupon_post_meta['exclude_product_ids'] [ 0 ] : '';
        $individual_use                    = isset( $coupon_post_meta['individual_use'] [ 0 ] ) && $coupon_post_meta['individual_use'] [ 0 ] != '' ? 'yes' : $coupon_post_meta['individual_use'] [ 0 ];
        $coupon_free_shipping              = isset( $coupon_post_meta['free_shipping'] [ 0 ] ) && $coupon_post_meta['free_shipping'] [ 0 ] != '' ? $coupon_post_meta['free_shipping'] [ 0 ] : 'no';
        $coupon_minimum_amount             = isset( $coupon_post_meta['minimum_amount'] [ 0 ] ) && $coupon_post_meta['minimum_amount'] [ 0 ] != '' ? $coupon_post_meta['minimum_amount'] [ 0 ] : '';
        $coupon_maximum_amount             = isset( $coupon_post_meta['maximum_amount'] [ 0 ] ) && $coupon_post_meta['maximum_amount'] [ 0 ] != '' ? $coupon_post_meta['maximum_amount'] [ 0 ] : '';
        $coupon_exclude_sale_items         = isset( $coupon_post_meta['exclude_sale_items'] [ 0 ] ) && $coupon_post_meta['exclude_sale_items'] [ 0 ] != '' ? $coupon_post_meta['exclude_sale_items'] [ 0 ] : 'no';
        $use_limit                         = isset( $coupon_post_meta['usage_limit'] [ 0 ] ) && $coupon_post_meta['usage_limit'] [ 0 ] != '' ? $coupon_post_meta['usage_limit'] [ 0 ] : '';
        $use_limit_user                    = isset( $coupon_post_meta['usage_limit_per_user'] [ 0 ] ) && $coupon_post_meta['usage_limit_per_user'] [ 0 ] != '' ? $coupon_post_meta['usage_limit_per_user'] [ 0 ] : '';
        if( class_exists( 'WC_Free_Gift_Coupons' ) ) {
            $free_gift_coupon              = isset( $coupon_post_meta['gift_ids'] [ 0 ] ) && $coupon_post_meta['gift_ids'] [ 0 ] != '' ? $coupon_post_meta['gift_ids'] [ 0 ] : '';
            $free_gift_shipping            = isset( $coupon_post_meta['free_gift_shipping'] [ 0 ] ) && $coupon_post_meta['free_gift_shipping'] [ 0 ] != '' ? $coupon_post_meta['free_gift_shipping'] [ 0 ] : 'no';
        }
        $amount        = $discount_amt;
        $discount_type = $get_discount_type; // Type: fixed_cart, percent, fixed_product, percent_product
        $coupon        = array(
            'post_title'   => $coupon_code,
            'post_content' => 'This coupon provides 5% discount on cart price.',
            'post_status'  => 'publish',
            'post_author'  => 1,
            'post_type'    => 'shop_coupon',
            'post_expiry_date' => $datetime,
        );
        $new_coupon_id = wp_insert_post( $coupon );
        // Add meta
        update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
        update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
        update_post_meta( $new_coupon_id, 'minimum_amount', $coupon_minimum_amount );
        update_post_meta( $new_coupon_id, 'maximum_amount', $coupon_maximum_amount );
        update_post_meta( $new_coupon_id, 'individual_use', $individual_use );
        update_post_meta( $new_coupon_id, 'free_shipping', $coupon_free_shipping );
        update_post_meta( $new_coupon_id, 'product_ids', '' );
        update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
        update_post_meta( $new_coupon_id, 'usage_limit', $use_limit );
        update_post_meta( $new_coupon_id, 'usage_limit_per_user', $use_limit_user );
        update_post_meta( $new_coupon_id, 'expiry_date', $datetime );
        update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
        update_post_meta( $new_coupon_id, 'product_ids', $coupon_product_ids );
        update_post_meta( $new_coupon_id, 'exclude_sale_items', $coupon_exclude_sale_items );
        update_post_meta( $new_coupon_id, 'exclude_product_ids', $coupon_exclude_product_ids );
        update_post_meta( $new_coupon_id, 'product_categories', $coupon_product_categories );
        update_post_meta( $new_coupon_id, 'exclude_product_categories', $coupon_exculde_product_categories );
        if( class_exists( 'WC_Free_Gift_Coupons' ) ) {
            update_post_meta( $new_coupon_id, 'gift_ids', $free_gift_coupon );
            update_post_meta( $new_coupon_id, 'free_gift_shipping', $free_gift_shipping );
        }
        return $final_string;
    }
    /**
     * It is used for generating random coupon code which will send in the abandoned cart reminder email.
     *
     * @return array $temp_array Coupon code
     * @since 4.2
     */
    public static function wp_random_string() {
        $character_set_array   = array();
        $character_set_array[] = array( 'count' => 5, 'characters' => 'abcdefghijklmnopqrstuvwxyz' );
        $character_set_array[] = array( 'count' => 5, 'characters' => '0123456789' );
        $temp_array            = array();
        foreach ( $character_set_array as $character_set ) {
            for ( $i = 0; $i < $character_set['count']; $i++ ) {
                $temp_array[] = $character_set['characters'][ rand( 0, strlen( $character_set['characters'] ) - 1 ) ];
            }
        }
        shuffle( $temp_array );
        return implode( '', $temp_array );
    }
}
API documentation generated by ApiGen