<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/

/**
 * Disable payment gateway based on category.
 */
function ace_disable_payment_gateway_category( $gateways ) {
    // Categories that'll disable the payment gateway 
	$category_slugs = array( 'pine-tree', 'another-category' );
	$category_ids = get_terms( array( 'taxonomy' => 'product_cat', 'slug' => $category_slugs, 'fields' => 'ids' ) );

	// Check each cart item for given category
	foreach ( WC()->cart->get_cart() as $item ) {
        $product = $item['data'];
	$product = wc_get_product( $product->get_parent_id() ?: $product );

		if ( $product && array_intersect( $category_ids, $product->get_category_ids() ) ) {
			unset( $gateways['bacs'] ); // Disable payment gateway 'cod' when product has one of the categories
            break;
		}
	}

	return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'ace_disable_payment_gateway_category' );