HI @aleksdab,
Your code is mostly correct, but there are a few adjustments to consider. The shipping method check uses 'flat_rate:3'
, but depending on how your shipping methods are configured, the flat_rate
shipping method might not be recognized as 'flat_rate:3'
. It’s important to verify that you’re using the correct identifier.
In the code, you’re using unset( $available_gateways['bacs'] )
and unset( $available_gateways['cod'] )
. The 'bacs'
payment gateway ID is for bank transfers, while 'cod'
typically refers to the “Cash on Delivery” method. If you’re trying to remove both the “bank transfer” and “quick payment” gateways, make sure you’re using the correct gateway IDs. For example, 'bacs'
is the ID for bank transfers.
The second unset( $available_gateways['cod'] )
won’t work as expected since it’s likely referring to the “Cash on Delivery” method itself. You’ll need to specify the gateway ID for the quick payment method, such as 'quick_pay_gateway'
(replace it with the actual ID for the quick payment gateway).
Also, the WC()->session->get( 'chosen_shipping_methods' )
may not always be available when the filter is triggered. It’s a good idea to ensure that the session is loaded and that a shipping method is selected before checking.
Here’s an updated version of the code with the changes:
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateways_based_on_chosen_shipping_method' );
function payment_gateways_based_on_chosen_shipping_method( $available_gateways ) {
// Make sure the session is available and the shipping method is set
if ( ! WC()->session ) {
return $available_gateways;
}
// Get the chosen shipping method(s)
$chosen_shipping_methods = (array) WC()->session->get( 'chosen_shipping_methods' );
// Check if "flat_rate:3" is chosen as the shipping method
if ( in_array( 'flat_rate:3', $chosen_shipping_methods ) ) {
// Unset bank transfer (bacs) and the quick payment gateway (replace with actual gateway ID)
unset( $available_gateways['bacs'] );
unset( $available_gateways['quick_pay_gateway'] ); // Replace with the actual ID of the quick payment gateway
}
return $available_gateways;
}
Make sure the 'flat_rate:3'
identifier matches the shipping method setup in WooCommerce and replace 'quick_pay_gateway'
with the correct ID for the quick payment gateway.
If the issue persists, it might be helpful to hire a developer to review your setup and provide the correct code, as this forum focuses on WooCommerce core issues.