Skip to content
Advertisement

Change Checkout “Billing Details” text for a specific product in Woocommerce

How can I change the text “Billing Details” in Woocommerce checkout page only for a specific product?

I have tried:

/* Change the Billing Details checkout label to Your Details: */
function wc_billing_field_strings( $translated_text, $text, $domain ) 
{
switch ( $translated_text ) {
case 'Billing Details' :
$translated_text = __( 'Your Details', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'wc_billing_field_strings', 20, 3 );

But it’s changing the text for all products… How could I do this for one specific product?

Advertisement

Answer

To change a translatable text in checkout page when there is a specific item in cart, use the following:

add_filter(  'gettext',  'change_conditionally_checkout_heading_text', 10, 3 );
function change_conditionally_checkout_heading_text( $translated, $text, $domain  ) {
    if( $text === 'Billing details' && is_checkout() && ! is_wc_endpoint_url() ){
        // HERE set the desired specific product ID
        $targeted_product_id = 1980;

        // Loop through cart items
        foreach( WC()->cart->get_cart() as $cart_item ) {
            if( $targeted_product_id == $cart_item['data']->get_id() ) {
                return __( 'Your Details', $domain );
                break; // Stop the loop
            }
        }
    }
    return $translated;
}

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

Note: In Woocommerce checkout the text to change is “Billing details” without a capital in “Details”

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement