php - 在 WooCommerce 结帐中更改特定产品的付款方式标题
问题描述
我正在尝试,当特定产品在结帐时,付款方式的标题会从;例如:“刷卡支付”改为“部分支付”,可以吗?
我试过用jquery:
jQuery(function($){
if ( $('#payment-fractional-payment').length ){
$("label[for='payment_method_redsys_gw']").text("Payment in installments");
}
});
但它只是改变了片刻并返回到默认标题,有没有办法在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 文件中。它应该有效。
推荐阅读
- php - 当我不知道空值的位置时从数据库中选择
- javascript - 如何使用单个键渲染组件?
- php - 将 SHA1 转换为 base64
- react-native - 如何从 React Native 中的自定义组件内部导航回父页面
- android - 如何将数据从 Bottomsheet 传递到 Jetpack Compose 中的活动?
- r - 如何在 excel/csv 中输入数据以在 R studio 中制作多个图表
- sql - 在 AWS Athena 中将小数转换为货币
- python - 识别车内车辆登记的轮廓区域
- javascript - Electron 14:TypeError:无法读取未定义的属性(读取“打开”)
- javascript - 将 fetch api 调用转换为 ajax