Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) {
Expand Down Expand Up @@ -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 );
}
}
Expand All @@ -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()) {
Expand Down Expand Up @@ -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 ) ) {
Expand Down
45 changes: 45 additions & 0 deletions src/OrderCoupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
if (!defined('ABSPATH')) exit;

use Rnoc\Retainful\Admin\Settings;
use Rnoc\Retainful\Helpers\Input;

class OrderCoupon
{
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 "<script>let is_local_storage = ( 'localStorage' in window && window.localStorage !== null );
let retainful_storage = window.localStorage.getItem('retainful_coupon_code');
if(is_local_storage && retainful_storage === null){
window.localStorage.setItem('retainful_coupon_code','$request_coupon_code');
}
</script>";
}
}

/**
* Create the virtual coupon
* @param $response
Expand Down
24 changes: 24 additions & 0 deletions src/assets/js/popup_coupon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

},
});
}
});