在 WooCommerce 结帐中更改特定产品的付款方式标题

Change payment method title for specific products in WooCommerce checkout

我正在尝试,当特定产品在结账时,付款方式的标题从;例如:“刷卡支付”改为“分批支付”,可以吗?

我试过 jquery:

jQuery(function($){
    if ( $('#payment-fractional-payment').length ){
        $("label[for='payment_method_redsys_gw']").text("Payment in installments");
    }
});

但它只改变了片刻,returns 变成了默认标题,有没有什么办法可以在 functions.php 中用一些钩子来做到这一点?

您可以使用这个简单的挂钩函数,您必须在其中设置正确的目标付款 ID 和目标产品 ID:

add_filter( 'woocommerce_gateway_title', 'change_payment_gateway_title', 100, 2 );
function change_payment_gateway_title( $title, $payment_id ){
    $targeted_payment_id  = 'redsys_gw'; // Set your payment method ID
    $targeted_product_ids = array(37, 53); // Set your product Ids

    // Only on checkout page for specific payment method Id
    if( is_checkout() && ! is_wc_endpoint_url() && $payment_id === $targeted_payment_id ) {
        // Loop through cart items
        foreach( WC()->cart->get_cart() as $item ) {
            // Check for specific products: Change payment method title
            if( in_array( $item['product_id'], $targeted_product_ids ) ) {
                return __("Payment in installments", "woocommerce");
            }
        }
    }
  return $title;
}

代码进入活动子主题(或活动主题)的 functions.php 文件。它应该有效。