3

In WooCommerce, I am looking for a function to change the "Place Order" text if cart has a specific product(ID) on checkout page.

This is useful for woo shops selling product and at the same time offering different services for example memberships. This will make the place order text more descriptive of the product as a Call to Action button.

I founded that function for change ‘add to cart' button text on single product page based on specific product id

add_filter( 'woocommerce_product_single_add_to_cart_text',
'woo_custom_cart_button_text' ); 

function woo_custom_cart_button_text( $text ) {
global $product;

if ( 123 === $product->id ) {
   $text = 'Product 123 text';
}
return $text;
}

And changing the place order text globally;

add_filter( 'woocommerce_order_button_text', 'woo_custom_order_button_text' ); 

function woo_custom_order_button_text() {
    return __( 'Your new button text here', 'woocommerce' ); 
}

Im looking for how to adapt them for checkout page.

Thanks.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
OnurK.
  • 121
  • 13

1 Answers1

2

If I have well understood your question, you got below a custom function that will display a custom text on Checkout submit button, when a specific product is in the cart:

add_filter( 'woocommerce_order_button_text', 'custom_checkout_button_text' );
function custom_checkout_button_text() {

    // Set HERE your specific product ID
    $specific_product_id = 37;
    $found = false;

    // Iterating trough each cart item
    foreach(WC()->cart->get_cart() as $cart_item)
        if($cart_item['product_id'] == $specific_product_id){
            $found = true; // product found in cart
            break; // we break the foreach loop
        }

    // If product is found in cart items we display the custom checkout button
    if($found)
        return __( 'Your new button text here', 'woocommerce' ); // custom text Here
    else
        return __( 'Place order', 'woocommerce' ); // Here the normal text
}

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

This code is tested and works.


Similar answers (multiple product IDs): WooCommerce - Check if item's are already in cart

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hello, thanks for reply. Function working fine for just specific product. Namely, if specific product is found in cart items we can display the custom checkout button. But, for NON-specific products we cant display any text on checkout button. So we have empty checkout button text for other produtcs. How can i fix that? Thank you so much again. – OnurK. Dec 27 '16 at 12:51
  • @OnurK. I have updated my answer… Now the button will not be empty anyway :) – LoicTheAztec Dec 27 '16 at 13:38
  • @LoicTheAztec The code worked on first run but it reverts back to the original "Place Order" text upon Ajax reload on the Order Summary. Screenshot: [link](https://anonfiles.com/97p298w1ua/Video_2021-05-17_141152_wmv) – Rosalito Udtohan May 17 '21 at 06:14