Skip to content

Commit 387149e

Browse files
committed
👌 IMPROVE: Updated redeem points ajax callback
1 parent 5393ed5 commit 387149e

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

customer-loyalty-for-woocommerce.php

+68
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,71 @@ function custom_update_notice() {
189189
</div>';
190190
}
191191
//add_action( 'admin_notices', 'custom_update_notice' );
192+
193+
/**
194+
* AJAX handler to redeem points and return a new coupon.
195+
*
196+
* @since 2.0.0
197+
* @return void
198+
*/
199+
function clwc_redeem_points_callback() {
200+
// Verify the nonce for security.
201+
if ( ! check_ajax_referer( 'clwc_redeem_nonce', 'nonce', false)) {
202+
wp_send_json_error( ['message' => __( 'Nonce verification failed', 'customer-loyalty-for-woocommerce' )] );
203+
return;
204+
}
205+
206+
$user_id = get_current_user_id();
207+
208+
// Check if the user is authenticated.
209+
if ( ! $user_id ) {
210+
wp_send_json_error( ['message' => __( 'User not authenticated', 'customer-loyalty-for-woocommerce' )] );
211+
return;
212+
}
213+
214+
// Fetch loyalty points and minimum redeemable points.
215+
$loyalty_points = get_user_meta( $user_id, 'clwc_loyalty_points', true ) ?: 0;
216+
$redeem_points_min = clwc_loyalty_points_redeem_points_minimum();
217+
218+
// Check if the user has enough points
219+
if ( $loyalty_points < $redeem_points_min ) {
220+
wp_send_json_error( ['message' => __( 'Insufficient points to redeem.', 'customer-loyalty-for-woocommerce' )] );
221+
return;
222+
}
223+
224+
// Generate a new coupon code.
225+
$coupon_code = clwc_get_random_string();
226+
$coupon_amount = clwc_loyalty_points_redeem_points_value();
227+
228+
// Create the coupon post.
229+
$coupon_id = wp_insert_post( [
230+
'post_title' => $coupon_code,
231+
'post_status' => 'publish',
232+
'post_type' => 'shop_coupon',
233+
'post_author' => $user_id,
234+
] );
235+
236+
if (is_wp_error( $coupon_id ) ) {
237+
wp_send_json_error( ['message' => __( 'Failed to create coupon.', 'customer-loyalty-for-woocommerce' )] );
238+
return;
239+
}
240+
241+
// Add meta data to the new coupon.
242+
update_post_meta( $coupon_id, 'discount_type', 'fixed_cart' );
243+
update_post_meta( $coupon_id, 'coupon_amount', $coupon_amount );
244+
update_post_meta( $coupon_id, 'usage_limit', '1' );
245+
246+
// Deduct points and get updated points.
247+
$updated_points = $loyalty_points - $redeem_points_min;
248+
update_user_meta( $user_id, 'clwc_loyalty_points', $updated_points );
249+
250+
// Generate HTML for the new coupon row.
251+
$new_coupon_html = sprintf(
252+
'<tr style="font-weight: bold; color: green;"><td><strong>%s</strong> - %s</td><td><span class="clwc-available-coupon">Available</span></td></tr>',
253+
esc_html( $coupon_code ),
254+
wc_price( $coupon_amount )
255+
);
256+
257+
wp_send_json_success( ['html' => $new_coupon_html, 'updated_points' => $updated_points] );
258+
}
259+
add_action( 'wp_ajax_clwc_redeem_points', 'clwc_redeem_points_callback' );

0 commit comments

Comments
 (0)