Search code examples
phpwordpresswoocommercecartpayment-method

WooCommerce custom payment gateway: Just show an extra fee based on total order


I am using WooCommerce and Woocrypt and in the payment gateway for bitcoins I have added the following code in my gateway:

<b class="bold-titel">Amount</b>
<span class="ngh-blocktext copywrap-address"><?php echo $order->get_total(); ?></span>

Ex, it shows, total amount to pay is ex: 50$ but I wait it to show 55$ (50$ + 10% without adding the code i inserted further down here which change total cart)

I am wondring is it possible to add 10% fee to the get_total command? i dont want to add 10% to the cart i only want the above code to display 10% extra. I have this to advertise to the customer that he pays the 10% extra fee if they get free item on there order.

I got:

add_action( 'woocommerce_cart_calculate_fees', 'custom_fee_based_on_cart_total', 10, 1 ); function
custom_fee_based_on_cart_total( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // The percentage
    $percent = 10; // 10%
    // The cart total
    $cart_total = $cart->cart_contents_total; 

    // The conditional Calculation
    $fee = $cart_total >= 25 ? $cart_total * $percent / 100 : 0;

    if ( $fee != 0 ) 
        $cart->add_fee( __( "Gratuity", "woocommerce" ), $fee, false ); 
}

which adds fee to total order.

I don't want this, I only want the above code to show the get_total with 10% added to the total amount.


Solution

  • Why don't display a message in the related payment gateway description (where you will need to set in the code below the correct payment Id).

    (You can also set this in the payment gateway description function instead)

    add_filter( 'woocommerce_gateway_description', 'gateway_bacs_custom_fields', 20, 2 );
    function gateway_bacs_custom_fields( $description, $payment_id ){
        // HERE below set the correct payment Id for your payment gateway
        if( 'bacs' === $payment_id )
        {
            $threshold_amount = 25; // threshold amount
            $percentage       = 10; // 10%
    
            $cart_subtotal    = WC()->cart->cart_contents_total;
            $cart_total       = WC()->cart->get_total('edit');
    
            if ( $cart_subtotal >= $threshold_amount ) {
    
                $message  = sprintf(
                    __('%s You will pay %s of extra fee, if you get free item on your order.'),
                    '<strong>' . __('Note:') . '</strong><br>',
                    '<strong>' . wc_price( $cart_total * $percentage / 100 ) . '</strong>',
                    $percentage . '%',
                    wc_price($cart_total)
                );
    
                $message .= '<br><em>' . sprintf(
                    __('This extra fee is %s of %s total order amount.'), $percentage . '%', wc_price($cart_total)
                ) . '</em>';
    
                $description .= '<div  class="message" style="padding:10px; margin-top:12px;; background-color: #fff; border: solid 1px #eee;">' . $message . '<div>';
            }
        }
        return $description;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.

    enter image description here


    You could alternatively display in checkout a message notice:

    add_action( 'woocommerce_before_checkout_form' , 'wc_minimum_order_amount' );
    function wc_minimum_order_amount() {
        $threshold_amount = 25; // threshold amount
        $percentage       = 10; // 10%
    
        $cart_subtotal    = WC()->cart->cart_contents_total;
        $cart_total       = WC()->cart->get_total('edit');
    
        if ( $cart_subtotal >= $threshold_amount ) {
            wc_print_notice( sprintf(
                __('You will pay %s  of extra fee (%s of %s total order amount), if you get free item on your order.'),
                wc_price( $cart_total * $percentage / 100 ), $percentage . '%', wc_price($cart_total)
            ) );
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.

    enter image description here