Search code examples
phpwordpresswoocommercecartpayment-method

Increase cart item prices based on payment method in WooCommerce


I want to add percentage value to cart item prices based on the selected payment gateway.

The problem I am facing is variation product price is not updating for the product price. Initially selected price is showing all the time.

How can I get the changed price accordingly?

My code so far:

// Set custom cart item price
function add_custom_price( $cart ) {

    // This is necessary for WC 3.0+
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook repetition (when using price calculations for example | optional)
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {

        $chosen_payment_method = WC()->session->get('chosen_payment_method');

        if($chosen_payment_method == 'cod') {
            $increaseby =  3;
        } elseif($chosen_payment_method == 'paypal') {
            $increaseby =  8;
        } else {
            $increaseby =  10;
        }

        $price = get_post_meta($cart_item['product_id'] , '_price', true);
        $price = $price + (($price * $increaseby)/100);
        $cart_item['data']->set_price( $price );
    }
}
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);

Any help highly appreciated.


Solution

  • There are some mistakes in your code

    • Use WC()->session->get( 'chosen_payment_method' ); outside the foreach loop
    • get_post_meta() is not needed to get the price, you can use get_price()
    • You will also need jQuery that is triggered when changing the payment method.

    So you get:

    function action_woocommerce_before_calculate_totals( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // Get payment method
        $chosen_payment_method = WC()->session->get( 'chosen_payment_method' );
    
        // Compare
        if ( $chosen_payment_method == 'cod' ) {
            $increaseby = 3;
        } elseif ( $chosen_payment_method == 'paypal' ) {
            $increaseby = 8;
        } else {
            $increaseby = 10;
        }
    
        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {     
            // Get price
            $price = $cart_item['data']->get_price();
    
            // Set price
            $cart_item['data']->set_price( $price + ( $price * $increaseby ) / 100 );
        }
    }
    add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );    
    
    function action_wp_footer() {
        if ( is_checkout() && ! is_wc_endpoint_url() ) :
        ?>
        <script type="text/javascript">
            jQuery(function($){
                $( 'form.checkout' ).on( 'change', 'input[name="payment_method"]', function() {
                    $(document.body).trigger( 'update_checkout' );
                });
            });
        </script>
        <?php
        endif;
    }
    add_action( 'wp_footer', 'action_wp_footer' );