Skip to content

Instantly share code, notes, and snippets.

@alexflorisca
Last active February 28, 2025 20:34
Show Gist options
  • Save alexflorisca/051e0fe9b0bae22f5c4efdae9c505ba9 to your computer and use it in GitHub Desktop.
Save alexflorisca/051e0fe9b0bae22f5c4efdae9c505ba9 to your computer and use it in GitHub Desktop.
Change order totals based on payment method
add_filter( 'woocommerce_calculated_total', 'modify_cart_total_by_payment_method', 10, 2 );
function modify_cart_total_by_payment_method( $total, $cart ) {
$chosen_payment_method = WC()->session->get( 'chosen_payment_method' );
switch ( $chosen_payment_method ) {
case 'bacs':
$total += 2;
break;
case 'cod':
$total += 3;
break;
case 'cheque':
$total += 5;
break;
default:
$modifier = 1;
break;
}
return $total;
}
add_action( 'woocommerce_store_api_checkout_update_order_from_request', 'update_totals_on_payment_method_change' );
function update_totals_on_payment_method_change( $post_data ) {
parse_str( $post_data, $post_data_array );
if ( isset( $post_data_array['payment_method'] ) ) {
WC()->session->set( 'chosen_payment_method', $post_data_array['payment_method'] );
WC()->cart->calculate_totals();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment