diff --git a/src/Main.php b/src/Main.php index 0aab6b94..c8dec06a 100644 --- a/src/Main.php +++ b/src/Main.php @@ -217,7 +217,8 @@ function activateEvents() { add_action( 'woocommerce_get_shop_coupon_data', array( $this->rnoc, 'addVirtualCoupon' ), 10, 2 ); add_action( 'rnoc_create_new_next_order_coupon', array( $this->rnoc, 'createNewCoupon' ), 10, 2 ); add_action( 'rnoc_initiated', array( $this->rnoc, 'setCouponToSession' ) ); - add_action( 'wp_loaded', array( $this->rnoc, 'addCouponToCheckout' ), 10 ); + add_action( 'wp_loaded', array( $this->rnoc, 'addCouponToCheckout' ) ); + //Attach coupon to email $hook = $this->admin->couponMessageHook(); if ( ! empty( $hook ) && $hook != "none" ) { @@ -304,8 +305,8 @@ function activateEvents() { add_action( 'wp_footer', array( $popup, 'printPopup' ) ); //apply popup coupon - add_action('wp_ajax_rnoc_apply_popup_coupon', array($popup, 'addPopupCouponToSession')); - add_action('wp_ajax_nopriv_rnoc_apply_popup_coupon', array($popup, 'addPopupCouponToSession')); // For non-logged-in users + add_action( 'wp_ajax_rnoc_apply_popup_coupon', array($popup, 'addPopupCouponToSession' ) ); + add_action( 'wp_ajax_nopriv_rnoc_apply_popup_coupon', array($popup, 'addPopupCouponToSession' ) ); // For non-logged-in users add_action( 'wp_loaded', array( $popup, 'applyPopupCoupon' ), 10 ); } } @@ -321,6 +322,10 @@ function activateEvents() { //add_action('wp_login', array($cart, 'userLoggedIn')); add_action( 'woocommerce_api_retainful', array( $cart, 'recoverUserCart' ) ); add_action( 'wp_loaded', array( $cart, 'applyAbandonedCartCoupon' ) ); + add_action( 'wp_ajax_rnoc_apply_coupon', array( $this->rnoc, 'applyCouponToCheckout' ) ); + add_action( 'wp_ajax_nopriv_rnoc_apply_coupon', array( $this->rnoc, 'applyCouponToCheckout' ) ); + add_action( 'wp_footer', array( $this->rnoc, 'setRnocCouponCode' ) ); + add_action( 'woocommerce_removed_coupon', array( $cart, 'removeNextOrderCouponFromCart' ) ); //Add tracking message /*if (is_user_logged_in()) { @@ -575,7 +580,7 @@ function doMigration() { /** * Show notices for user..if anything unusually happen in our plugin * - * @param string $message - message to notice users + * @param string $message - message to notice users */ function showAdminNotice( $message = "" ) { if ( ! empty( $message ) ) { diff --git a/src/OrderCoupon.php b/src/OrderCoupon.php index 4eaff577..f5d9163e 100644 --- a/src/OrderCoupon.php +++ b/src/OrderCoupon.php @@ -4,6 +4,7 @@ if (!defined('ABSPATH')) exit; use Rnoc\Retainful\Admin\Settings; +use Rnoc\Retainful\Helpers\Input; class OrderCoupon { @@ -242,6 +243,25 @@ public function addCouponToCheckout() } } + /** + * + * @return void + */ + public function applyCouponToCheckout() { + $input = new Input(); + $coupon_code = $input->get_post( 'retainful_coupon_code', '' ); + + if ( ! empty( $coupon_code ) && ! empty( $this->wc_functions->getCart() ) ) { + //Do not apply coupon until the coupon is valid + if ( $this->wc_functions->hasDiscount( $coupon_code ) ) { + wp_send_json_success( [ 'is_coupon_applied' => true ] ); + } elseif ( $this->checkCouponBeforeCouponApply( $coupon_code ) ) { + wp_send_json_success( [ 'is_coupon_applied' => $this->wc_functions->addDiscount( $coupon_code ) ] ); + } + } + wp_send_json_error( [ 'is_coupon_applied' => false ] ); + } + /** * show applied coupon popup */ @@ -394,6 +414,31 @@ function setCouponToSession() } } + + /** + * Set the retainful coupon into localStorage. + * + * @return void + * + */ + public function setRnocCouponCode() { + $request_coupon_code = null; + $input = new Input(); + if ( ! empty ( $input->get( 'retainful_ac_coupon' ) ) ) { + $request_coupon_code = $input->get( 'retainful_ac_coupon' ); + } elseif ( ! empty ( $input->get( 'retainful_coupon_code' ) ) ) { + $request_coupon_code = $input->get( 'retainful_coupon_code' ); + } + if ( ! empty( $request_coupon_code ) ) { + echo ""; + } + } + /** * Create the virtual coupon * @param $response diff --git a/src/assets/js/popup_coupon.js b/src/assets/js/popup_coupon.js index 8a026837..0616243f 100644 --- a/src/assets/js/popup_coupon.js +++ b/src/assets/js/popup_coupon.js @@ -45,4 +45,28 @@ jQuery(document).ready(function(){ } } }, retainful_popup_data.popup_redirect_timeout); +}); + +jQuery(document).ready(function () { + let coupon_code = localStorage.getItem('retainful_coupon_code'); + if (coupon_code) { + let data = { + action: "rnoc_apply_coupon", + retainful_coupon_code: localStorage.getItem('retainful_coupon_code') + }; + jQuery.ajax({ + type: "POST", + url: retainful_popup_data.ajax_url, + data: data, + dataType: "json", + success: function (response) { + if (response.data?.is_coupon_applied === true) { + localStorage.removeItem('retainful_coupon_code') + } + }, + error: function (response) { + + }, + }); + } }); \ No newline at end of file