Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce: Change text on order button

I am using WooCommerce for a nonprofit website and want to change the "Place Order" button text to say "Place Donation". The button is defined in WooCommerce's payment.php file:

<?php echo apply_filters( 'woocommerce_order_button_html', 
    '<input type="submit" class="button alt" name="woocommerce_checkout_place_order" 
    id="place_order" value="' . esc_attr( $order_button_text ) . 
    '" data-value="' . esc_attr( $order_button_text ) . '" />' ); ?>

I added the following to my functions.php file in the child theme:

function custom_order_button_text($order_button_text){
    $order_button_text = 'Place Donation';

    return $order_button_text;
}
add_filter('woocommerce_order_button_text', 'custom_order_button_text');

It momentarily seems to work, but changes back to 'Place Order' before the page finishes loading. The output HTML ends up as:

<input type="submit" class="button alt" name="woocommerce_checkout_place_order" 
id="place_order" value="Place order" data-value="Place Donation">

*Update: I turned off javascript and found that the button then said "Place Donation." I then found a script in woocommerce/assets/js/frontend/checkout.js as part of payment_method_selected

if ( $( this ).data( 'order_button_text' ) ) {
    $( '#place_order' ).val( $( this ).data( 'order_button_text' ) );
} else {
    $( '#place_order' ).val( $( '#place_order' ).data( 'value' ) );
}

Not sure the best way to override this. Any ideas?

like image 988
dpruth Avatar asked Mar 12 '23 01:03

dpruth


2 Answers

You can change the "Place Order" button text to say "Place Donation" using simple woocommerce hook as below..

/* Add to the functions.php file of your theme/plugin */

add_filter( 'woocommerce_order_button_text', 'wc_custom_order_button_text' ); 

function wc_custom_order_button_text() {
    return __( 'Place Donation', 'woocommerce' ); 
}

Hope this will help to someone else. Thanks

like image 131
Ibnul Hasan Avatar answered Mar 24 '23 12:03

Ibnul Hasan


Just came across the same issue myself. What I did to solve it is close to your solution.

Instead of dequeueing it completely I dequeued it and uploaded the excact same script to my child theme + commented out

 `/* if ( $( this ).data( 'order_button_text' ) ) {
    $( '#place_order' ).val( $( this ).data( 'order_button_text' ) );
} else {
    $( '#place_order' ).val( $( '#place_order' ).data( 'value' ) );
}*/ `

The PHP:

`add_action('wp_enqueue_scripts', 'override_woo_frontend_scripts');
function override_woo_frontend_scripts() {
    wp_deregister_script('wc-checkout');
    wp_enqueue_script('wc-checkout', get_template_directory_uri() . '/../storefront-child-theme-master/woocommerce/checkout.js', array('jquery', 'woocommerce', 'wc-country-select', 'wc-address-i18n'), null, true);
}    `
like image 37
billy Avatar answered Mar 24 '23 14:03

billy