首页 > 解决方案 > 在 WooCommerce 结帐中更改特定产品的付款方式标题

问题描述

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

我试过用jquery:

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

但它只是改变了片刻并返回到默认标题,有没有办法在functions.php中使用一些钩子来做到这一点?

标签: phpwoocommercehook-woocommercecheckoutpayment-method

解决方案


您可以使用这个简单的挂钩函数,您必须在其中设置正确的目标付款 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 文件中。它应该有效。


推荐阅读