Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Created March 26, 2026 07:53
Show Gist options
  • Select an option

  • Save xlplugins/f0aa3500c78ceb46f402e557434b5987 to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/f0aa3500c78ceb46f402e557434b5987 to your computer and use it in GitHub Desktop.
Reward & coupon according to currency
add_filter( 'fkcart_rewards_list', function ( $data ) {
if ( empty( $data['rewards'] ) || ! isset( $data['subtotal'] ) ) {
return $data;
}
$currency = function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : '';
$subtotal = (float) $data['subtotal'];
// (discount = coupon reward, freegift = free gift reward).
$targets = [
'USD' => [ 'discount' => 180, 'freegift' => 220 ],
'EUR' => [ 'discount' => 200, 'freegift' => 250 ],
'GBP' => [ 'discount' => 160, 'freegift' => 200 ],
];
if ( ! isset( $targets[ $currency ] ) ) {
return $data;
}
$row = $targets[ $currency ];
$data['coupons'] = [ 'add' => [], 'remove' => [] ];
$data['gifts'] = [ 'add' => [], 'remove' => [] ];
foreach ( $data['rewards'] as $i => $reward ) {
$type = isset( $reward['type'] ) ? $reward['type'] : '';
if ( ! isset( $row[ $type ] ) ) {
continue;
}
$need = (float) $row[ $type ];
$ok = ( $subtotal >= $need );
$data['rewards'][ $i ]['achieved'] = $ok;
$data['rewards'][ $i ]['pending_amount'] = $ok ? 0.0 : max( 0.0, $need - $subtotal );
if ( 'discount' === $type && ! empty( $reward['coupon'] ) ) {
if ( $ok ) {
$data['coupons']['add'][] = $reward['coupon'];
} else {
$data['coupons']['remove'][] = $reward['coupon'];
}
}
if ( 'freegift' === $type && ! empty( $reward['freeProduct'] ) && is_array( $reward['freeProduct'] ) ) {
$ids = array_column( $reward['freeProduct'], 'key' );
if ( $ok ) {
$data['gifts']['add'] = array_merge( $data['gifts']['add'], $ids );
} else {
$data['gifts']['remove'] = array_merge( $data['gifts']['remove'], $ids );
}
}
}
foreach ( [ 'coupons', 'gifts' ] as $bucket ) {
foreach ( [ 'add', 'remove' ] as $side ) {
$data[ $bucket ][ $side ] = array_values( array_unique( $data[ $bucket ][ $side ] ) );
}
}
return $data;
}, 20 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment