I have a requirement to update the selected product(s) price based on the selected payment gateway. There are predefined values that need to apply to each product(s) in the cart.
Ex: if PayPal is selected price needs to increase by 2.5%.
This is my code so far which is placed in the functions.php
file. But I can't see this is working as expected. Can anybody guide me through?
function add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
// get product id & price
$product = wc_get_product( $product_id );
$price = $product->get_price();
$selected_payment_method_id = WC()->session->get( 'chosen_payment_method' );
if($selected_payment_method_id == 'bacs') {
$cart_item_data['new_price'] = $price;
} elseif($selected_payment_method_id == 'cod') {
$cart_item_data['new_price'] = $price + 50;
} elseif($selected_payment_method_id == 'cheque') {
$cart_item_data['new_price'] = $price + 100;
} elseif($selected_payment_method_id == 'paypal') {
$cart_item_data['new_price'] = $price + 200;
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 10, 3 );