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

[亡魂溺海] 提交于 2020-08-26 13:50:38

问题


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?


回答1:


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"




回答2:


First, override the Woocommerce plugin template and change inside it wp-content/plugins/woocommerce/templates/checkout/form-checkout.php

find your necessary template and change the text.



来源:https://stackoverflow.com/questions/55389458/change-checkout-billing-details-text-for-a-specific-product-in-woocommerce

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