The woocommerce store is in operation. Do you want to know how to disable payment methods for specific categories or multiple payment methods based on the product categories in the shopping cart? If you are looking for a solution focusing on product categories, but you can also change the delivery level of the product itself with a slight change, this document will guide you how to achieve this. Disabling certain types of payment methods is a good way. A good example is that woocommerce store owners want to settle through bank remittances rather than customers using credit cards. This is just one example of many situations where you may need to disable payment methods for categories.
If you are not proficient in technology, you can choose advanced plug-ins to perform operations. For example, the woocommerce conditional payment plug-in helps create conditions that enable or disable payment methods. If you are not good at using code, this will be the best advanced solution you need to consider. In addition, this plug-in allows you to define payment gateways and items that are not displayed. These gateways can create and use shopping cart totals, billing countries \/ regions, user roles, etc. without restrictions. However, in this simple tutorial, we will show you how to use PHP code fragments to disable specific categories of payment methods.
Simple steps to disable specific categories of payment methods using PHP snippets include: Log in to the WordPress site and access the dashboard as an administrator. In the panel menu, click shape Menu > theme editor menu. If the topic editor page opens, browse the topic features file to add features that disable specific categories of payment methods. Add the following code to the PHP file\/**
*@Snippet disables certain types of payment methods
*\/
add_filter( ‘woocommerce_available_payment_gateways’, ‘njengah_unset_gateway_by_category’ );
Function njengah_unset_gateway _by_category ($available_gateways){
Return if (is_admin() $available_gateways.
Return if (! Is_checkout() $available_gateways.
$unset = false;
$category_ids = array (8,37);
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
$terms = get_the_terms( $values[‘product_id’], ‘product_cat’ );
foreach ( $terms as $term ) {
if ( in_array( $term->term_id, $category_ids ) ) {
$unset = fact;
Fracture
}
}
}
if ( $unset == true ) unset( $available_gateways[‘cheque’] );
$available_gateways returns;
}This will be the result. For the code to work, you must add the correct category ID in the woocommerce store.
If you are not satisfied with the use of category ID, woocommerce stores can also use category tracking. It is easy to understand and easy to refer to in the future, so you can choose to use it. Use the category tracker to log in to the WordPress website at the stage where the payment method for a specific category is disabled.
Access the dashboard as an administrator. In the panel menu, click shape Menu > theme editor menu. If the topic editor page opens, browse the topic features file to add features that disable specific categories of payment methods. Add the following code to the PHP file\/**
*@Disable payment gateway according to snippet category.
*\/
Function njengah_disable_payment_gate_category ($gateways){
\/\/Category to disable payment gateway
$category_slugs = array (\
$category _ids = get _terms (array (\
\/\/View each shopping cart item for a given category.
foreach ( WC()->cart->get_cart() as $item ) {
$product = $item [\
if ( $product && array_intersect( $category_ids, $product->get_category_ids() ) ) {
unset( $gateways[‘cod’] ); \/\/ If the product has a category, disable the settlement gateway \
Fracture
}
}
Return $gateway;
}
add_filter( ‘woocommerce_available_payment_gateways’, ‘njengah_disable_payment_gateway_category’ ); Remember that this will be the result. You must define the payment gateway to add and disable the correct category tracking. For example, use \
Note that when the specified category is in the shopping cart, you must change the payment gateway identifier to the item you want to disable. Bank remittance (BACs) check (check) payment repayment (payment repayment) PayPal, but even if one of the basic payment gateways is not used, there is a simple way to find the payment gateway ID, so don’t worry. You can go to the payment gateway settings page to do this and find the ID in it. For example, for the cash on delivery option on the settings page, the URL is WP admin \/ Admin. php? page=wc-settings&tab=checkout§ion=cod。
Conclusion this post learned how to use two methods to disable specific categories of payment methods. For future reference, you can use product category ID or category tracking to define. To add this feature, don’t forget to use the category ID or serial number from the woocommerce store. Similar to the article WRAC woocommerce check-out plug-in, the method of changing the button text attached to the shopping cart on the redirect woocommerce shop page, the method of deleting the menu, the method of my account page woocommerce woocommerce, the method of selling digital products, the method of basic product sorting woocommerce, the method of deleting the product GTIN number in woocommerce Added method for better user experience, the method of sorting woocommerce categories, the method of hiding all products in the shopping page of woocommerce, the method of hiding the coupon code field of woocommerce, the method of hiding the quantity field in the product page of woocommerce, or the method of displaying woocommerce products by deleted category, add the product image Method woocommerce settlement page