I need to provide BIN number based discount in WooCommerce. I have tried to calculate discount and to apply coupon but none of it works while on the order-pay page. If i go back to checkout it applies the discount. I need this discount to be applied while on the order-pay.
Here is the AJAX POST
$.ajax({
type: 'POST',
dataType: 'json',
url: ajaxUrl,
data: {
action: 'apply_coupon_based_on_card_number',
coupon_code: '99vzm2fp' // or whatever
},
success: function(response) {
console.log(response);
if (response.success) {
console.log('Coupon applied successfully:', response.data);
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
action: 'update_order_review',
payment_method: paymentGateway
},
success: function(updateResponse) {
console.log('Order review updated successfully.');
$('.woocommerce-checkout-review-order-table').html(updateResponse.fragments['.woocommerce-checkout-review-order-table']);
// Append the updated order details
var orderDetails = `
<p>Order ID: ${response.data.order_id}</p>
<p>Subtotal: ${response.data.subtotal}</p>
<p>Discount: ${response.data.discount}</p>
<p>Shipping: ${response.data.shipping}</p>
<p>Total: ${response.data.total}</p>
`;
$('.order_details').append(orderDetails);
},
error: function(error) {
console.error('Error updating order review:', error);
}
});
} else {
console.log('Coupon not applied:', response.data.message);
// Append the order details even if coupon not applied
var orderDetails = `
<p>Order ID: ${response.data.order_id}</p>
<p>Subtotal: ${response.data.subtotal}</p>
<p>Discount: ${response.data.discount}</p>
<p>Shipping: ${response.data.shipping}</p>
<p>Total: ${response.data.total}</p>
`;
$('.order_details').append(orderDetails);
}
},
error: function(xhr, status, error) {
console.error('Error applying coupon:', error);
console.log('XHR:', xhr);
console.log('Status:', status);
}
});
and here is the PHP
function apply_coupon_based_on_card_number() {
// Check if coupon is already applied
if ( WC()->session->get( 'coupon_applied' ) ) {
wp_send_json_error([
'message' => 'Coupon already applied.',
'order_id' => WC()->session->get('order_awaiting_payment'),
'subtotal' => WC()->cart->get_subtotal(),
'discount' => WC()->cart->get_cart_discount_total(),
'shipping' => WC()->cart->get_shipping_total(),
'total' => WC()->cart->total,
]);
}
// Get the coupon code from the request
$coupon_code = isset($_POST['coupon_code']) ? sanitize_text_field($_POST['coupon_code']) : '';
// Check if the coupon code exists and apply it
if ( !empty($coupon_code) && WC()->cart->apply_coupon( $coupon_code ) ) {
// Mark the coupon as applied in the session
WC()->session->set( 'coupon_applied', true );
// Send a success response with the updated cart details
wp_send_json_success([
'message' => 'Coupon applied successfully.',
'order_id' => WC()->session->get('order_awaiting_payment'),
'subtotal' => WC()->cart->get_subtotal(),
'discount' => WC()->cart->get_cart_discount_total(),
'shipping' => WC()->cart->get_shipping_total(),
'total' => WC()->cart->total,
]);
} else {
// Send an error response if coupon application fails
wp_send_json_error([
'message' => 'Coupon could not be applied.',
'order_id' => WC()->session->get('order_awaiting_payment'),
'subtotal' => WC()->cart->get_subtotal(),
'discount' => WC()->cart->get_cart_discount_total(),
'shipping' => WC()->cart->get_shipping_total(),
'total' => WC()->cart->total,
]);
}
wp_die();
}
add_action( 'wp_ajax_apply_coupon_based_on_card_number', 'apply_coupon_based_on_card_number' );
add_action( 'wp_ajax_nopriv_apply_coupon_based_on_card_number', 'apply_coupon_based_on_card_number' );
Like i said, tried calculating 10% discount also, could not apply it. I keep getting ‘Coupon already applied.’ message but nothing actually happens unless i go back to checkout where coupon shows and only then is applied.