Skip to content

Commit 5393ed5

Browse files
committed
👌 IMPROVE: Updated redeem functionality on frontend in my account page
1 parent 528cefa commit 5393ed5

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

public/class-clwc-public.php

+20-3
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,25 @@ public function enqueue_styles() {
7575
* @since 1.0.0
7676
* @return void
7777
*/
78-
public function enqueue_scripts() {
79-
//wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/clwc-public.js', array( 'jquery' ), $this->version, false );
78+
public function enqueue_scripts($hook_suffix) {
79+
// Ensure this is only enqueued on the relevant page or template
80+
if (is_account_page()) { // Modify condition as per your requirements
81+
$user_id = get_current_user_id();
82+
83+
wp_enqueue_script(
84+
'clwc-redeem-script',
85+
plugin_dir_url(__FILE__) . 'js/clwc-redeem.js',
86+
['jquery'],
87+
time(),
88+
true
89+
);
90+
91+
wp_localize_script('clwc-redeem-script', 'clwc_ajax', [
92+
'ajax_url' => admin_url('admin-ajax.php'),
93+
'user_id' => $user_id,
94+
'nonce' => wp_create_nonce('clwc_redeem_nonce')
95+
]);
96+
}
8097
}
81-
98+
8299
}

public/js/clwc-redeem.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
jQuery(document).ready(function ($) {
2+
$('#clwc-redeem-points').on('click', function (e) {
3+
e.preventDefault();
4+
console.log('Redeem button clicked'); // Debugging line
5+
6+
$.ajax({
7+
url: clwc_ajax.ajax_url,
8+
method: 'POST',
9+
data: {
10+
action: 'clwc_redeem_points',
11+
nonce: clwc_ajax.nonce,
12+
user_id: clwc_ajax.user_id
13+
},
14+
success: function (response) {
15+
console.log(response); // Debugging line
16+
if (response.success) {
17+
// Prepend new coupon to the table
18+
$('#clwc-coupons-table tbody').prepend(response.data.html);
19+
20+
// Display success message
21+
$('#clwc-redeem-points').after('<p class="clwc-redeem-success" style="color: green;">Coupon redeemed successfully!</p>');
22+
setTimeout(function () {
23+
$('.clwc-redeem-success').fadeOut();
24+
}, 3000);
25+
26+
// Update the loyalty points display
27+
$('.clwc-loyalty-points-total').text(response.data.updated_points);
28+
} else {
29+
alert(response.data.message);
30+
}
31+
},
32+
error: function (xhr, status, error) {
33+
console.error('AJAX error:', status, error); // Debugging line
34+
alert('An error occurred. Please try again.');
35+
}
36+
});
37+
});
38+
});

0 commit comments

Comments
 (0)