WooCommerce Payments Gateway标题的自定义文本

发布于 2025-01-17 15:17:22 字数 736 浏览 1 评论 0原文

我试图将WooCommerce付款的标签从“流行付款方式”更改为结帐页面上的其他内容。

我将此片段添加到儿童主题的 function.php

add_filter( 'woocommerce_gateway_title', 'change_payment_gateway_title', 100, 2 );
function change_payment_gateway_title( $title, $payment_id ){
    if( $payment_id === 'woocommerce_payments' ) {
        $title = __("custom text", "woocommerce");
    }
    return $title;
}

add_filter( 'woocommerce_gateway_title', 'change_payment_gateway_title' );
function change_payment_gateway_title( $payment_id ){
    return str_replace( 'Popular payment methods', 'custom text', $payment_id );
}

当结帐页面仍在加载时,它们都可以使用一秒钟,但标题跳回原始文本(“流行”付款方式”)页面完成加载后。他们可以在其他付款方式上没有任何问题。

我的代码有什么问题吗?

I was trying to change the label of WooCommerce Payments from "Popular payment methods" to something else on the checkout page.

I added this snippet to functions.php of the child theme

add_filter( 'woocommerce_gateway_title', 'change_payment_gateway_title', 100, 2 );
function change_payment_gateway_title( $title, $payment_id ){
    if( $payment_id === 'woocommerce_payments' ) {
        $title = __("custom text", "woocommerce");
    }
    return $title;
}

and this

add_filter( 'woocommerce_gateway_title', 'change_payment_gateway_title' );
function change_payment_gateway_title( $payment_id ){
    return str_replace( 'Popular payment methods', 'custom text', $payment_id );
}

They both work for a fraction of a second when the checkout page is still loading, but the title jumps back to the original text ("Popular payment methods") when the page has finished loading. They work without any problem for other payment methods though.

Is there anything wrong with my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜未央樱花落 2025-01-24 15:17:22

恢复的原始文本源自 woocommerce payment 创建的 js 变量“wcpay_config”。它包含在键“checkoutTitle”中并通过 wp_localize_script
因为我没有找到连接此函数的方法,所以我使用 这个答案 实现一个子类和您的第一个解决方案相结合,实现完全替换:

class Filterable_Scripts extends WP_Scripts
{
    function localize( $handle, $object_name, $l10n ) {
        $l10n = apply_filters( 'script_word_replacements', $l10n, $handle, $object_name );
        return parent::localize($handle, $object_name, $l10n);
    }
}

add_action( 'init', function() {
    $fscripts = new Filterable_Scripts();

    $GLOBALS['wp_scripts'] = $fscripts;
});

add_filter('script_word_replacements', 'woocommerce_payments_name', 10 , 3);

function woocommerce_payments_name($l10n, $handle, $object_name ) {
    if('wcpay-upe-checkout' == $handle && 'wcpay_config' == $object_name) {
        $l10n[checkoutTitle] = __("Custom name for woocommerce payments", "woocommerce");
    }
    return $l10n;
}

如果其他插件停止工作,另请参阅答案。

The original text getting restored originates from the js variable "wcpay_config" that is created by woocommerce payments. It is contained in the key "checkoutTitle" and passed through wp_localize_script.
Because I didn't find a way to hook into this function I used this answer implementing a subclass and your first solution combined, to achieve a full replacement:

class Filterable_Scripts extends WP_Scripts
{
    function localize( $handle, $object_name, $l10n ) {
        $l10n = apply_filters( 'script_word_replacements', $l10n, $handle, $object_name );
        return parent::localize($handle, $object_name, $l10n);
    }
}

add_action( 'init', function() {
    $fscripts = new Filterable_Scripts();

    $GLOBALS['wp_scripts'] = $fscripts;
});

add_filter('script_word_replacements', 'woocommerce_payments_name', 10 , 3);

function woocommerce_payments_name($l10n, $handle, $object_name ) {
    if('wcpay-upe-checkout' == $handle && 'wcpay_config' == $object_name) {
        $l10n[checkoutTitle] = __("Custom name for woocommerce payments", "woocommerce");
    }
    return $l10n;
}

Also take a look at this answer, if other plugins stop working.

空袭的梦i 2025-01-24 15:17:22
add_filter( 'woocommerce_gateway_title', 'change_payment_gateway_title', 25, 2 );

function change_payment_gateway_title( $title, $gateway_id ){
    
    if( 'gateway_id' === $gateway_id ) {
        $title = 'By Cash or Credit Card on delivery';
    }

    return $title;
}

什么是$ gateway_id,从哪里得到它?我认为最简单的方法是使用此代码snippet < /a>在WooCommerce设置中添加付款网关ID列。

add_filter( 'woocommerce_gateway_title', 'change_payment_gateway_title', 25, 2 );

function change_payment_gateway_title( $title, $gateway_id ){
    
    if( 'gateway_id' === $gateway_id ) {
        $title = 'By Cash or Credit Card on delivery';
    }

    return $title;
}

What is $gateway_id and where to get it? I think the easiest way is to use this code snippet which adds a payment gateway ID column in WooCommerce settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文