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 
<?php
/**
 * It will capture the coupon codes and apply the coupon code directly when ac reminder emails have the coupon.
 * @author   Tyche Softwares
 * @package Abandoned-Cart-Pro-for-WooCommerce/Frontend/Coupon
 * @since 5.0
 */
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

if ( !class_exists('Wcap_Coupon' ) ) {
    /**
     * It will capture the coupon codes and apply the coupon code directly when ac reminder emails have the coupon
     */
    class Wcap_Coupon{

        /**
         * It will captures the coupon code used by the customers.
         * It will store the coupon code for the specific abandoned cart.
         * @hook woocommerce_applied_coupon
         * @param string $valid Coupon code
         * @return string $valid Coupon code
         * @globals mixed $wpdb
         * @since 2.4.3
         */
        public static function wcap_capture_applied_coupon( $valid ) {
            if( session_id() === '' ){
                //session has not started
                session_start();
            }

            global $wpdb;

            $coupon_code = '';
            if ( isset( $_SESSION['wcap_user_id'] ) && $_SESSION['wcap_user_id'] > 0 ) {
                $user_id = $_SESSION ['wcap_user_id'];
            } else {
                $user_id = get_current_user_id();
            }

            if( isset( $_SESSION['acp_c'] ) && $_SESSION['acp_c'] != '' ) {
                $coupon_code = $_SESSION['acp_c'];
            } elseif (isset($_POST['coupon_code'])){

                $coupon_code = $_POST['coupon_code'];
            }else if (isset($valid)){
                $coupon_code = $valid;
            }

            if ( $valid != '' ) {
                if ( is_user_logged_in()){

                    $abandoned_cart_id_query   = "SELECT id FROM `" . WCAP_ABANDONED_CART_HISTORY_TABLE . "` WHERE user_id = %d AND cart_ignored = '0' AND recovered_cart = '0'";
                    $abandoned_cart_id_results = $wpdb->get_results( $wpdb->prepare( $abandoned_cart_id_query, $user_id ) );
                }else if ( ! is_user_logged_in()){
                        $wcap_get_cookie_id        =  Wcap_Common::wcap_get_guest_session_key();
                        $abandoned_cart_id_query   = "SELECT id FROM `" . WCAP_ABANDONED_CART_HISTORY_TABLE . "` WHERE user_id = %d ";
                        $abandoned_cart_id_results = $wpdb->get_results( $wpdb->prepare( $abandoned_cart_id_query, $user_id ) );
                }

                $abandoned_cart_id         = '0';
                if( isset( $abandoned_cart_id_results ) && !empty( $abandoned_cart_id_results ) ) {
                    $abandoned_cart_id = $abandoned_cart_id_results[0]->id;
                }
                $existing_coupon = ( get_user_meta( $user_id, '_woocommerce_ac_coupon', true ) );
                    if ( is_array( $existing_coupon ) && count( $existing_coupon ) > 0 ) {
                        foreach ( $existing_coupon as $key => $value ) {
                            if ( $existing_coupon[$key]['coupon_code'] != $coupon_code ) {
                                $existing_coupon[]      = array ( 'coupon_code' => $coupon_code, 'coupon_message' => __( 'Discount code applied successfully.', 'woocommerce-ac' ) );
                                $post_meta_coupon_array = array ( 'coupon_code' => $coupon_code, 'coupon_message' => __( 'Discount code applied successfully.', 'woocommerce-ac' ) );
                                update_user_meta( $user_id, '_woocommerce_ac_coupon', $existing_coupon );
                                if( $user_id > 0 || ! is_user_logged_in() ) {
                                    add_post_meta( $abandoned_cart_id, '_woocommerce_ac_coupon', $post_meta_coupon_array );
                                }
                            return $valid;
                        }
                    }
                } else {
                    $coupon_details[]       = array ( 'coupon_code' => $coupon_code, 'coupon_message' => __( 'Discount code applied successfully.', 'woocommerce-ac' ) );
                    $post_meta_coupon_array = array ( 'coupon_code' => $coupon_code, 'coupon_message' => __( 'Discount code applied successfully.', 'woocommerce-ac' ) );
                    update_user_meta( $user_id, '_woocommerce_ac_coupon', $coupon_details );

                    if( $user_id > 0  || ! is_user_logged_in() ) {
                        add_post_meta( $abandoned_cart_id, '_woocommerce_ac_coupon', $post_meta_coupon_array );
                    }
                    return $valid;
                }
            }

            return $valid;
        }

        /**
         * It will captures the coupon code errors specific to the abandoned carts.
         * @hook woocommerce_coupon_error
         * @param string $valid Error
         * @param string $new Error code
         * @globals mixed $wpdb
         * @return string $valid Error
         * @since 2.4.3
         */
        public static function wcap_capture_coupon_error( $valid, $new ) {
            if( session_id() === '' ){
                //session has not started
                session_start();
            }
            global $wpdb;
            $coupon_code = '';
            if ( isset( $_SESSION['wcap_user_id'] ) && $_SESSION['wcap_user_id'] > 0 ) {
                $user_id = $_SESSION ['wcap_user_id'];
            } else {
                $user_id = get_current_user_id();
            }
            $abandoned_cart_id_query   = "SELECT id FROM `" . WCAP_ABANDONED_CART_HISTORY_TABLE . "` WHERE user_id = %d AND cart_ignored = '0' AND recovered_cart = '0'";
            $abandoned_cart_id_results = $wpdb->get_results( $wpdb->prepare( $abandoned_cart_id_query, $user_id ) );
            $abandoned_cart_id         = '0';

            if( isset( $abandoned_cart_id_results ) && count( $abandoned_cart_id_results ) > 0 ) {
                $abandoned_cart_id = $abandoned_cart_id_results[0]->id;
            }
            if( isset( $_SESSION ['acp_c'] ) && $_SESSION ['acp_c'] != '' ) { // check here
                $coupon_code = $_SESSION ['acp_c'];
            } else {
                if( isset( $_POST['coupon_code'] ) ) {
                    $coupon_code = $_POST['coupon_code'];
                }
            }
            if ( '' != $coupon_code ) {
                $existing_coupon        = get_user_meta( $user_id, '_woocommerce_ac_coupon', true );
                $existing_coupon[]      = array ( 'coupon_code' => $coupon_code, 'coupon_message' => $valid );
                $post_meta_coupon_array = array ( 'coupon_code' => $coupon_code, 'coupon_message' => $valid );
                if ( $user_id > 0 ) {
                    add_post_meta( $abandoned_cart_id, '_woocommerce_ac_coupon', $post_meta_coupon_array );
                }
                update_user_meta( $user_id, '_woocommerce_ac_coupon', $existing_coupon );
            }
            return $valid;
        }
        /**
         * It will directly apply the coupon code if the coupon code present in the abandoned cart reminder email link.
         * It will apply direct coupon on cart and checkout page.
         * @hook woocommerce_before_cart_table
         * @hook woocommerce_before_checkout_form
         * @param string $coupon_code Name of coupon
         * @since 2.4.3
         */
        public static function wcap_apply_direct_coupon_code( $coupon_code ) {
            if( session_id() === '' ){
                //session has not started
                session_start();
            }

            remove_action( 'woocommerce_cart_updated', array( 'Wcap_Cart_Updated', 'wcap_store_cart_timestamp' ) );
            if ( isset( $_SESSION['wcap_selected_language'] ) && $_SESSION['wcap_selected_language'] !='' && function_exists( 'icl_register_string' ) ) {
                global $sitepress;
                $sitepress->switch_lang( $_SESSION ['wcap_selected_language'] );
            }

            if ( isset( $_SESSION['acp_c'] ) && $_SESSION['acp_c'] != '' ) {
                global $woocommerce;
                $coupon_code = $_SESSION['acp_c'];
               // If coupon has been already been added remove it.
                if ( $woocommerce->cart->has_discount( sanitize_text_field( $coupon_code ) ) ) {
                    if ( !$woocommerce->cart->remove_coupons( sanitize_text_field( $coupon_code ) ) ) {
                        wc_print_notices( );
                    }
                }
                // Add coupon
                if ( !$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ) ) ) {
                    wc_print_notices( );
                } else {
                    wc_print_notices( );
                }
                // Manually recalculate totals.  If you do not do this, a refresh is required before user will see updated totals when discount is removed.
                $woocommerce->cart->calculate_totals();
                // need to clear the coupon code from session
                $_SESSION['acp_c'] = '';
            }
       }
    }
}
API documentation generated by ApiGen