John Davidson

php - Change payment method title for specific products in WooCommerce checkout

0 comments
Message:


I'm trying that, when a specific product is in the checkout, the title of the payment method changes from; for example: "Payment by card" to "Payment in parts", is it possible?


I have tried with jquery:


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

but it only changes it for a moment and returns to the default title, is there any way to do it in the functions.php with some hook?



My Answer:
"Credit Card" to "Bank Transfer".

Here's how you can achieve this using a custom function in your theme's functions.php file:

php
add_filter( 'woocommerce_gateway_title', 'change_payment_method_title', 10, 2 );
function change_payment_method_title( $title, $payment_id ) {
// Check if the specific product is in the cart
$specific_product_id = 123; // Replace 123 with the ID of your specific product
$cart = WC()->cart->get_cart();
foreach ( $cart as $cart_item ) {
if ( $cart_item['product_id'] == $specific_product_id ) {
// Change the title for the specific product
if ( $payment_id === 'your_payment_method_id' ) { // Replace 'your_payment_method_id' with the ID of the payment method you want to change
$title = 'Bank Transfer';
}
}
}
return $title;
}


In this code snippet, replace `123` with the ID of your specific product and `your_payment_method_id` with the ID of the payment method you want to change the title for. This function will check if the specific product is in the cart and if the payment method matches the ID you specified, it will change the title to "Bank Transfer".

Make sure to test this code on a staging site before implementing it on your live site.

Rate this post

3 of 5 based on 3451 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us