To remove other payment methods when the customer selects the “pickup” shipping method in WooCommerce, you can use the woocommerce_available_payment_gateways filter in your theme’s functions.php file or in a custom plugin. Here’s an example:
function remove_payment_methods_for_pickup($available_gateways) {
// Check if the customer has selected the "pickup" shipping method
if (WC()->session->get('chosen_shipping_methods')[0] === 'pickup') {
// Remove all other payment methods except for "Prepayment"
unset($available_gateways['bacs']); // Replace 'bacs' with the desired payment method slug
unset($available_gateways['cheque']); // Replace 'cheque' with the desired payment method slug
// Add any other payment methods you want to remove
// Optional: Set the default payment method to "Prepayment"
WC()->session->set('chosen_payment_method', 'bacs'); // Replace 'bacs' with the desired payment method slug
}
return $available_gateways;
}
add_filter('woocommerce_available_payment_gateways', 'remove_payment_methods_for_pickup', 10, 1);
In the code above, the remove_payment_methods_for_pickup function checks if the customer has selected the “pickup” shipping method. If so, it removes all other payment methods except for “Prepayment” (in this case, the “bacs” payment method). You can modify the code to match the slugs of your desired payment methods.
Optionally, you can set the default payment method to “Prepayment” by using the WC()->session->set('chosen_payment_method', 'bacs') line. Adjust the slug accordingly.
Add this code to your theme’s functions.php file or create a custom plugin for it. Save the changes, and the other payment methods will be hidden when the customer selects the “pickup” shipping method.
Please note that the code assumes the payment method slugs and shipping method slugs are as mentioned in the example. Adjust the slugs accordingly based on your actual setup.