Menu Close

Disable Payment Method If Product Category – Woocommere

codeamend

Take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case:

  1. Find list on product categories based on all the products in the Cart
  2. Disable a specific payment ID if the target product category is in the Cart.
Disable Payment Method for Specific Category – WooCommerce Checkout
add_filter( 'woocommerce_available_payment_gateways', codeamend_unset_gateway_by_category);
  
function codeamend_unset_gateway_by_category( $available_gateways ) {
    if ( is_admin() ) return $available_gateways;
    if ( ! is_checkout() ) return $available_gateways;
    $unset = false;
    $category_id = 8; // TARGET CATEGORY
    foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
        $terms = get_the_terms( $values['product_id'], 'product_cat' );	
        foreach ( $terms as $term ) {    	
            if ( $term->term_id == $category_id ) {
                $unset = true; 
                // CATEGORY IS IN THE CART
                break;
            }
        }
    }
    if ( $unset == true ) unset( $available_gateways['cod'] );
    return $available_gateways;
}
 
Note to find payment gateway slug: When you hover the payment gateway name in Woocommerce payment settings you can find refer: https://example.com/wp-admin/admin.php?page=wc-settings&tab=checkout&section=paypal

Share

Posted in WooCommerce

You can also read...

Leave a Reply

Your email address will not be published.