更改结帐“帐单明细” Woocommerce中特定产品的文本 [英] Change Checkout "Billing Details" text for a specific product in Woocommerce

查看:70
本文介绍了更改结帐“帐单明细” Woocommerce中特定产品的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅在Woocommerce结帐页面上更改文本结算明细?

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

我尝试过:

/* 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?

推荐答案

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

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

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

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


注意:在Woocommerce结帐中,要更改的文字是结算明细。在 D etails中没有大写字母

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

这篇关于更改结帐“帐单明细” Woocommerce中特定产品的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆