This code allows you to enable or disable specific payment gateways for certain product categories in WooCommerce.
Steps and Functions
- Plugin Setup
Create a new folder in the wp-content/plugins directory named woocommerce-toggle-payments-by-category. Inside this folder, create a PHP file named woocommerce-toggle-payments-by-category.php.
<?php
/**
* Plugin Name: WooCommerce Toggle Payments By Category
* Description: Enable or disable payment gateways for specific product categories in WooCommerce.
* Version: 1.0
* Author: Your Name
* License: GPL2
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
class WC_Toggle_Payments_By_Category {
public function __construct() {
add_filter('woocommerce_available_payment_gateways', [$this, 'filter_payment_gateways'], 10, 1);
add_action('admin_menu', [$this, 'create_admin_menu']);
add_action('admin_init', [$this, 'register_settings']);
}
public function filter_payment_gateways($available_gateways) {
if (is_admin()) return $available_gateways;
$restricted_gateways = get_option('wc_tpb_restricted_gateways', []);
if (empty($restricted_gateways)) return $available_gateways;
$cart_items = WC()->cart->get_cart();
foreach ($cart_items as $cart_item) {
$product_id = $cart_item['product_id'];
$terms = get_the_terms($product_id, 'product_cat');
foreach ($terms as $term) {
if (isset($restricted_gateways[$term->term_id])) {
foreach ($restricted_gateways[$term->term_id] as $gateway_id) {
if (isset($available_gateways[$gateway_id])) {
unset($available_gateways[$gateway_id]);
}
}
}
}
}
return $available_gateways;
}
public function create_admin_menu() {
add_options_page(
'Toggle Payments by Category',
'Toggle Payments by Category',
'manage_options',
'wc-toggle-payments-by-category',
[$this, 'settings_page']
);
}
public function register_settings() {
register_setting('wc_tpb_settings_group', 'wc_tpb_restricted_gateways', [
'sanitize_callback' => [$this, 'sanitize_restricted_gateways'],
'default' => []
]);
}
public function sanitize_restricted_gateways($input) {
// Sanitize input as needed
foreach ($input as $key => $value) {
$input[$key] = array_map('sanitize_text_field', $value);
}
return $input;
}
public function settings_page() {
$categories = get_terms('product_cat', ['hide_empty' => false]);
$payment_gateways = WC()->payment_gateways->payment_gateways();
$restricted_gateways = get_option('wc_tpb_restricted_gateways', []);
?>
<div class="wrap">
<h1>Toggle Payments by Category</h1>
<form method="post" action="options.php">
<?php settings_fields('wc_tpb_settings_group'); ?>
<table class="form-table">
<?php foreach ($categories as $category) : ?>
<tr valign="top">
<th scope="row"><?php echo esc_html($category->name); ?></th>
<td>
<?php foreach ($payment_gateways as $gateway) : ?>
<label>
<input type="checkbox" name="wc_tpb_restricted_gateways[<?php echo esc_attr($category->term_id); ?>][]" value="<?php echo esc_attr($gateway->id); ?>" <?php checked(in_array($gateway->id, $restricted_gateways[$category->term_id] ?? [])); ?>>
<?php echo esc_html($gateway->get_title()); ?>
</label><br>
<?php endforeach; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
}
new WC_Toggle_Payments_By_Category();
?>
Explanation of Code
- Plugin Header: The header contains the plugin’s name, description, version, author, and license information. This is required for WordPress to recognize the plugin.
- Exit if accessed directly: This prevents direct access to the file for security purposes.
- Class Definition: The main class
WC_Toggle_Payments_By_Categoryencapsulates all the functionality. - Constructor: The constructor hooks into
woocommerce_available_payment_gatewaysto filter available payment gateways and hooks intoadmin_menuandadmin_initto create and manage the plugin settings page. - filter_payment_gateways Method: This method checks the product categories in the cart and disables payment gateways based on the settings. It retrieves the restricted gateways from the plugin’s options and removes them from the available gateways.
- create_admin_menu Method: This method adds an options page under the Settings menu in the WordPress admin.
- register_settings Method: This method registers the plugin’s settings.
- settings_page Method: This method generates the settings page where the admin can configure which payment gateways to disable for each product category. It retrieves all product categories and payment gateways and displays them in a table format with checkboxes.
Usage
- Activate the Plugin: Go to the WordPress admin, navigate to Plugins, and activate the “WooCommerce Toggle Payments By Category” plugin.
- Configure Settings: Navigate to Settings -> Toggle Payments by Category, and configure which payment gateways to disable for each product category by checking the appropriate boxes.
- Effect in Checkout: When a product from a category with restricted payment gateways is added to the cart, the restricted payment gateways will be removed from the available options at checkout.
This code provides a flexible way to control payment gateways based on product categories, enhancing the WooCommerce store’s functionality.