Asked  1 Year ago    Answers:  5   Viewed   6 times

In Woocommerce, we are trying to add an additional cost to the order when is purchased via Paypal payment gateway.

We did it changing the price that is sent to Paypal this way:

add_filter('woocommerce_paypal_args', 'addition_pay');

function addition_pay($paypal_args){
        $new_value=$paypal_args['amount_1']+10;
        $paypal_args['amount_1']=$new_value;
        return $paypal_args;
} 

It works, but the problem is after the payment process, this additional cost is not reflected in Orders and email notifications.

Is this can be solved in some way? Any help is appreciated.

 Answers

5

You should better add a fee based on payment gateway (here Paypal for you), like in the following:

// Add a fee of 10.00 when Paypal is chosen
add_action( 'woocommerce_cart_calculate_fees', 'custom_paypal_additional_fee', 20, 1 );
function custom_paypal_additional_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if( WC()->session->get( 'chosen_payment_method' ) == 'paypal' )
        $cart->add_fee( __( 'Paypal fee', 'woocommerce' ), 10.00 );
}

// Add the information on checkout paypal text gateways section
add_filter('woocommerce_gateway_icon', 'custom_paypal_gateway_text', 20, 2 );
function custom_paypal_gateway_text( $html, $gateway_id ) {
    if( $gateway_id == 'paypal' )
        $html .= ' <small class="paypal-fee">(+ '.wc_price(10.00).')</small>';

    return $html;
}

// Enable ajax update checkout event when choosing a gateway to refresh the fee
add_action('wp_footer', 'payment_gateways_update_checkout_event' );
function payment_gateways_update_checkout_event() {
    ?>
    <script type="text/javascript">
        (function($){
            $('form.checkout').on( 'change', 'input[name^="payment_method"]', function() {
                var t = { updateTimer: !1,  dirtyInput: !1,
                    reset_update_checkout_timer: function() {
                        clearTimeout(t.updateTimer)
                    },  trigger_update_checkout: function() {
                        t.reset_update_checkout_timer(), t.dirtyInput = !1,
                        $(document.body).trigger("update_checkout")
                    }
                };
                $(document.body).trigger('update_checkout')
            });
        })(jQuery);
    </script>
    <?php
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.

Thursday, April 1, 2021
 
Pradip
 
2

Your code is a bit obsolete since WooCommerce version 3. Now on cart page, PHP redirect is useless, because Emptying cart is an Ajax customer live event. So in this case Javascript (jQuery) is required.

Use the following instead, that handle all cases:

add_action( 'template_redirect', 'empty_cart_redirection' );
function empty_cart_redirection(){
    if( is_cart() ) :
    
    // Here set the Url redirection
    $url_redirection = get_permalink( wc_get_page_id( 'shop' ) );
    
    // When trying to access cart page if cart is already empty  
    if( WC()->cart->is_empty() ){
        wp_safe_redirect( $url_redirection );
        exit();
    }
    
    // When emptying cart on cart page
    wc_enqueue_js( "jQuery(function($){
        $(document.body).on( 'wc_cart_emptied', function(){
            if ( $( '.woocommerce-cart-form' ).length === 0 ) {
                $(window.location).attr('href', '" . $url_redirection . "');
                return;
            }
        });
    });" );
    endif;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works in all WooCommerce versions since version 3.

Thursday, April 1, 2021
 
Wilk
 
3

Updated

This can be done very easily using dedicated woocommerce_checkout_coupon_message filter hook:

add_filter( 'woocommerce_checkout_coupon_message', 'custom_checkout_coupon_message', 20, 1 );
function custom_checkout_coupon_message( $message ) {
    ?>
        <script type='text/javascript'>
            jQuery(document).ready( function($){
                setTimeout(function(){
                    $('form.checkout_coupon').css('display','block');
                }, 300);
            });
        </script>
    <?php
    // HERE your custom message
    return __( 'Have a coupon?', 'woocommerce' ) . ' <a class="showcoupon-off">' . __( 'Below you can enter your coupon code', 'woocommerce' ) . '</a>';
}

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

Friday, May 28, 2021
 
turik
 
5

Here is a way to change the button text and the button color depending on the product category:

// Change add to cart button text per category
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_single_add_to_cart_text', 20, 2 );
function custom_single_add_to_cart_text( $text_button, $product ) {
    global $post;
    $domain = 'woocommerce';

    // HERE set the array of categories terms slug and corresponding colors and texts
    $categories_colors = array(
        'clothing'  => array( 'color' => 'red', 'text' => __('Order your Clothes', $domain ) ),
        'posters'   =>  array( 'color' => 'blue', 'text' => __('Order your Posters', $domain ) ),
    );

    $color = '';
    $text_button = __('Order now', $domain ); // default

    foreach ($categories_colors as $key => $value ) {
        if( has_term( $key, 'product_cat', $post->ID ) ){
            $color = $value['color'];
            $text_button = $value['text'];
            break;
        }
    }
    if ( empty($color) ) return $text_button; // Exit if empty

    // Dynamic css andjquery to set the color when defined
    ?>
    <style>
        button.single_add_to_cart_button.<?php echo $color; ?>{background-color:<?php echo $color; ?> !important;}
    </style>
    <?php
    // jQuery (add color css class)
    ?>
    <script type="text/javascript">
        (function($){
             $('button.single_add_to_cart_button').addClass('<?php echo $color; ?>');
        })(jQuery);
    </script>
    <?php

    return $text_button;
}

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

You can remove the entire <style> bock in the code adding your own css rules in your active theme styles.css file (based on the color added css class)

Saturday, May 29, 2021
 
Whakkee
 
5

To display Your custom message in checkout page before the Woocommerce login message and before add coupon message, you will use the following code instead:

add_action('template_redirect', 'my_custom_message');
function my_custom_message() {
    if ( ! is_user_logged_in() && is_checkout() && ! is_wc_endpoint_url() ) {
        wc_add_notice( sprintf( __('This is my <strong>"custom message"</strong> and I can even add a button to the right… <a href="%s" class="button alt">My account</a>'), get_permalink( get_option('woocommerce_myaccount_page_id') ) ), 'notice' );
    }
}

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


A more simpler version for all users in checkout page only:

add_action('template_redirect', 'my_custom_message');
function my_custom_message() {
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        wc_add_notice( __('This is my custom message'), 'notice' );
    }
}

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

Saturday, May 29, 2021
 
pinaki
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :