Change checkout submit button text for a specific payment method in WooCommerce

℡╲_俬逩灬. 提交于 2020-07-07 17:53:48

问题


I need to change the order_button_text for a specific payment gateway (COD in this case).

I could only get it to change globally (for all payment gateways) using:

add_action( 'woocommerce_after_add_to_cart_button', 'multiple_orders_text' );
function woo_custom_order_button_text() {
    return __( 'Request Shipping Quote', 'woocommerce' );
}

But have found that if I add the line

$this->order_button_text  = __( 'Request a Quote', 'woocommerce' );

to the setup_properties() method in woocommerce/includes/gateways/cod/class-wc-gateway-cod.php it does work.

However this is clearly bad practice as I'm hacking a core plugin file.

How can I achieve this without hacking a woocommerce core file?


回答1:


You can do it like this:

add_filter( 'woocommerce_available_payment_gateways', 'woocommerce_available_payment_gateways' );
function woocommerce_available_payment_gateways( $available_gateways ) {
    if (! is_checkout() ) return $available_gateways;  // stop doing anything if we're not on checkout page.
    if (array_key_exists('paypal',$available_gateways)) {
        // Gateway ID for Paypal is 'paypal'. 
         $available_gateways['paypal']->order_button_text = __( 'Request a Quote', 'woocommerce' );
    }
    return $available_gateways;
}

This code example is for paypal. For reference of the gateway IDs, please check WooCoomerce > Settings > Checkout > Gateway display order




回答2:


Here it is the clean way to do it, using woocommerce_review_order_before_payment action hook with a custom function hooked in, using mainly jQuery (because it's a client side live event):

add_action( 'woocommerce_review_order_before_payment', 'customizing_checkout_button', 10, 0 );
function customizing_checkout_button(){

    $text1  = __( 'Place order', 'woocommerce' );
    $text2  = __( 'Request a Quote', 'woocommerce' );

    ?>
    <script>
        jQuery(function($){

            // 1. Initialising once loaded
            if($('input[name^="payment_method"]:checked').val() == 'cod' )
                $('input[name^="woocommerce_checkout_place_order"]').val('<?php echo $text2; ?>');
            else
                $('input[name^="woocommerce_checkout_place_order"]').val('<?php echo $text1; ?>');

            // 2. Live event detection:When payment method is changed
            $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
                var choosenPaymentMethod = $('input[name^="payment_method"]:checked').val(); // Chosen

                if( choosenPaymentMethod == 'cod' )
                    $('input[name^="woocommerce_checkout_place_order"]').val('<?php echo $text2; ?>');
                else 
                    $('input[name^="woocommerce_checkout_place_order"]').val('<?php echo $text1; ?>');
            });
        });
    </script>
    <?php
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works with WooCommerce 3+



来源:https://stackoverflow.com/questions/45739331/change-checkout-submit-button-text-for-a-specific-payment-method-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!