Search code examples
phpwoocommercehook-woocommercecheckoutpayment-method

WooCommerce Disallow ClearPay Payment for specific product categories


I am trying to disable ClearPay payment method for specific product categories.

I managed to hide the ClearPay options for the shop and product page, but it still allows me to use ClearPay in the cart.

This is the code I have used:

/**
 * @param bool $bool_result
 * @param WC_Product $product
 */
function clearpay_ips_callback( $bool_result, $product ) {
    if( has_term( array( 18, 28 ), 'product_cat' ) ) {
        // do something if current product in the loop is in product category with ID 18 or 25
        $bool_result = false;
    }
    return $bool_result;
}
add_filter( 'clearpay_is_product_supported', 'clearpay_ips_callback', 10, 2 );

Can anyone help me to stop ClearPay being available in checkout if we have products that belong to specific product category term Ids


Solution

  • The product Id should be specified in has_term() like:

    add_filter( 'clearpay_is_product_supported', 'clearpay_ips_callback', 10, 2 );
    function clearpay_ips_callback( $bool_result, $product ) {
    
        if( has_term( array( 18, 28 ), 'product_cat', product->get_id() ) ) {
            return false;
        }
        return $bool_result;
    }
    

    You can also try the following for cart items on checkout page:

    add_filter( 'woocommerce_available_payment_gateways', 'conditional_hiding_payment_gateway', 1, 1 );
    function conditional_hiding_payment_gateway( $available_gateways ) {
        // Not in backend (admin)
        if( is_admin() ) 
            return $available_gateways;
    
        $is_in_cart = false;
    
        // Iterating through each items in cart
        foreach(WC()->cart->get_cart() as $item){
            if( has_term( array( 18, 28 ), 'product_cat', $item['product_id'] ) ) {
                $is_in_cart = true;
                break;  // stop the loop
            }
        }
        if( $is_in_cart )
            unset($available_gateways['clearpay']);
        }
        return $available_gateways;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should better work.