// Register a filter hook with a specific function name
add_filter('woocommerce_available_payment_gateways', 'customize_payment_gateway_based_on_criteria', 10, 1);
// Function to customize a payment gateway based on specific conditions
function customize_payment_gateway_based_on_criteria($available_gateways) {
// Check if in the admin area and return early
if (is_admin()) {
return $available_gateways;
}
// Retrieve the relevant criteria
$criteria_value = '';
if (is_object(WC()->customer)) {
$criteria_value = WC()->customer->get_specific_criteria(); // Update with the specific method
}
// Define the target condition as a lowercase string
$target_condition = 'your_target_condition';
// Check if the criteria matches the target condition
if (strtolower($criteria_value) === $target_condition) {
// Remove the payment gateway based on ID without creating a copy of the array
unset($available_gateways['your_payment_gateway_id']);
}
return $available_gateways;
}
//Notes:
// 1. Pass the number of arguments expected by the function as the fourth parameter in add_filter for better performance.
// 2. Use is_object() to check if WC()->customer is an object before accessing its method to avoid errors.
// 3. Avoid unnecessary array copying by directly unsetting the payment gateway from the $available_gateways array.
Associated Context | |
---|---|
Type | Code Snippet ( .php ) |
Associated Tags | add_filter function woocommerce_available_payment_gateways array bbloomer_payment_gateway_disable_country variable is_admin method WC class customer object get_billing_city attribute unset operator available_gateways['cod'] key Framework: WordPress wordpress woocommerce hook-woocommerce shipping wordpress-theming WooCommerce Hooks Payment Gateways Filter Function |
💡 Smart Description | This code snippet adds a filter to the 'woocommerce_available_payment_gateways' list. It checks if an admin user is enabled and disables country based on their billing city, then removes the customer's shipping city from that available gateways by setting it in This function disables the "Cash on Delivery" payment gateway for customers whose billing city is "Pantelleria" in WooCommerce, except for admin users. |
🔎 Suggested Searches | How to disable country in bbloomer payment gateway using WordPress? |
Related Links | https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/ https://wordpress.org/plugins/woocommerce/ |
Related People | Davide Ladisa |
Sensitive Information | No Sensitive Information Detected |
Shareable Link | https://davideladisa.pieces.cloud/?p=1e59408c41 |