0

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 );
WP Learner
  • 788
  • 1
  • 13
  • 34
  • you have to loop cart items before accessing - foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {$product = $cart_item['data']; $price = WC()->cart->get_product_price( $product );} – Snuffy Nov 09 '21 at 07:15
  • Also use woocommerce_available_payment_gateways hook since you want to edit prices based on payment methods – Snuffy Nov 09 '21 at 07:19

0 Answers0