更改 Woocommerce 中特定产品的结帐 "Billing Details" 文本

Change Checkout "Billing Details" text for a specific product in Woocommerce

如何仅针对特定产品更改 Woocommerce 结帐页面中的文本 "Billing Details"?

我试过:

/* 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 );

但它正在更改所有产品的文本……我如何才能针对某一特定产品执行此操作?

首先,覆盖 Woocommerce 插件模板并在其中进行更改 wp-content/plugins/woocommerce/templates/checkout/form-checkout.php

找到您需要的模板并更改文本。

要在购物车中有特定商品时更改结帐页面中的可翻译文本,请使用以下内容:

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;
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

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