Skip to content

Instantly share code, notes, and snippets.

@TanvirHasan19
Created November 3, 2025 10:27
Show Gist options
  • Select an option

  • Save TanvirHasan19/e3fc51f45033e44a0eea4168d413e571 to your computer and use it in GitHub Desktop.

Select an option

Save TanvirHasan19/e3fc51f45033e44a0eea4168d413e571 to your computer and use it in GitHub Desktop.
Limit Points Redemption to Percentage of Cart Value
// ==============================================================================
// LPFW Percentage Limit - Limit Points Redemption to Percentage of Cart Value
// ==============================================================================
add_action('admin_footer', function() {
$screen = get_current_screen();
if (!$screen || (strpos($screen->id, 'woocommerce') === false && strpos($screen->id, 'acfw') === false)) {
return;
}
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
function injectInlineField() {
if ($('#lpfw_inline_percentage_field').length > 0) {
return; // Already added
}
// Look for ANY form table or settings container
var $container = $('table.form-table, .acfw-settings-content, .lpfw-settings-content, #wpbody-content .wrap').first();
if ($container.length > 0) {
var fieldHtml = '<div id="lpfw_inline_percentage_field" style="margin: 20px 0; padding: 20px; background: #f0f8ff; border: 2px solid #2271b1; border-radius: 5px;">' +
'<h3 style="margin: 0 0 10px 0; color: #2271b1;">🎯 Points Redemption Percentage Limit</h3>' +
'<p style="margin: 0 0 10px 0;"><strong>Maximum percentage of cart value payable with points:</strong></p>' +
'<input type="number" id="lpfw_inline_input" value="<?php echo esc_js(get_option('lpfw_max_points_redeem_percentage', 50)); ?>" min="0" max="100" step="1" style="width: 100px; padding: 8px; font-size: 16px;" /> % ' +
'<button type="button" id="lpfw_inline_save" class="button button-primary">Save Setting</button>' +
'<p style="margin: 10px 0 0 0; color: #666; font-size: 13px;">Controls how much of an order can be paid with loyalty points. Example: 50 = up to 50% of cart total.</p>' +
'<div id="lpfw_inline_msg" style="margin-top: 10px;"></div>' +
'</div>';
if ($container.prop('tagName') === 'TABLE') {
$container.before(fieldHtml);
} else {
$container.prepend(fieldHtml);
}
// Save handler
$('#lpfw_inline_save').on('click', function() {
var val = $('#lpfw_inline_input').val();
$.post(ajaxurl, {
action: 'lpfw_save_percentage_limit',
value: val,
nonce: '<?php echo wp_create_nonce('lpfw_percentage_nonce'); ?>'
}, function(res) {
if (res.success) {
$('#lpfw_inline_msg').html('<span style="color: green; font-weight: bold;">✅ Saved successfully!</span>').fadeOut(3000);
}
});
});
}
}
// Try multiple times
setTimeout(injectInlineField, 100);
setTimeout(injectInlineField, 500);
setTimeout(injectInlineField, 1500);
});
</script>
<?php
}, 999);
// AJAX handler to save the percentage value
add_action('wp_ajax_lpfw_save_percentage_limit', function() {
check_ajax_referer('lpfw_percentage_nonce', 'nonce');
if (!current_user_can('manage_woocommerce')) {
wp_send_json_error('Permission denied');
}
$value = floatval($_POST['value']);
$value = max(0, min(100, $value)); // Clamp between 0-100
update_option('lpfw_max_points_redeem_percentage', $value);
wp_send_json_success(array('value' => $value));
});
// Set percentage (change 50 to your desired percentage)
if ( ! defined( 'LPFW_MAX_POINTS_REDEEM_PERCENTAGE' ) ) {
define( 'LPFW_MAX_POINTS_REDEEM_PERCENTAGE', 50 ); // 50% of cart value
}
// Get the maximum percentage setting
function lpfw_get_max_points_redeem_percentage() {
return defined( 'LPFW_MAX_POINTS_REDEEM_PERCENTAGE' )
? floatval( LPFW_MAX_POINTS_REDEEM_PERCENTAGE )
: floatval( get_option( 'lpfw_max_points_redeem_percentage', 50 ) );
}
// Calculate maximum points allowed based on cart percentage
function lpfw_calculate_max_points_by_percentage( $cart_subtotal ) {
$max_percentage = lpfw_get_max_points_redeem_percentage();
if ( $max_percentage <= 0 || $max_percentage >= 100 ) {
return PHP_INT_MAX; // No limit
}
$max_cart_value_redeemable = ( $cart_subtotal * $max_percentage ) / 100;
$redeem_ratio = (int) get_option( 'acfw_loyalprog_redeem_points_ratio', 10 );
return intval( $max_cart_value_redeemable * $redeem_ratio );
}
// Limit store credits amount based on cart percentage
add_filter( 'acfw_store_credits_redeem_amount', 'lpfw_limit_store_credits_by_cart_percentage', 10, 2 );
function lpfw_limit_store_credits_by_cart_percentage( $amount, $cart_total ) {
$max_percentage = lpfw_get_max_points_redeem_percentage();
if ( $max_percentage <= 0 || $max_percentage >= 100 ) {
return $amount;
}
$max_amount_redeemable = ( $cart_total * $max_percentage ) / 100;
return min( $amount, $max_amount_redeemable );
}
// Validate points redemption percentage limit
add_filter( 'acfw_is_valid_store_credits_redeem_amount', 'lpfw_validate_points_percentage_limit', 10, 3 );
function lpfw_validate_points_percentage_limit( $is_valid, $amount, $cart_total ) {
if ( true !== $is_valid ) {
return $is_valid;
}
$max_percentage = lpfw_get_max_points_redeem_percentage();
if ( $max_percentage <= 0 || $max_percentage >= 100 ) {
return $is_valid;
}
$max_amount_redeemable = ( $cart_total * $max_percentage ) / 100;
if ( $amount > $max_amount_redeemable ) {
return new WP_Error(
'lpfw_points_exceed_percentage_limit',
sprintf(
__( 'You can only use points to pay for up to %s%% of your order total (maximum %s).', 'loyalty-program-for-woocommerce' ),
$max_percentage,
wc_price( $max_amount_redeemable )
),
array(
'status' => 400,
'amount' => $amount,
'max_amount_redeemable' => $max_amount_redeemable,
'cart_total' => $cart_total,
)
);
}
return $is_valid;
}
// ==============================================================================
// END LPFW Percentage Limit
// ==============================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment