【问题标题】:Change payment method title for specific products in WooCommerce checkout在 WooCommerce 结帐中更改特定产品的付款方式标题
【发布时间】:2021-05-27 03:14:41
【问题描述】:
我正在尝试,当特定产品在结帐时,付款方式的标题会从;例如:“刷卡支付”改为“部分支付”,可以吗?
我用 jquery 试过了:
jQuery(function($){
if ( $('#payment-fractional-payment').length ){
$("label[for='payment_method_redsys_gw']").text("Payment in installments");
}
});
但它只是改变了片刻并返回到默认标题,有什么方法可以在functions.php中通过一些钩子来做到这一点?
【问题讨论】:
标签:
php
woocommerce
hook-woocommerce
checkout
payment-method
【解决方案1】:
您可以使用这个简单的挂钩函数,您必须在其中设置正确的目标付款 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 文件中。它应该可以工作。