Ad

Remove Checkout Terms & Conditions Conditionally In WooCommerce

I want to remove the Woocommerce Privacy Policy text and the terms & conditions during checkout if there are no available payment gateways.

This is the code that I tried

add_action('woocommerce_checkout_terms_and_conditions', 'disable_woocommerce_checkout_options', 10 );
function disable_woocommerce_checkout_options(){
    if ( empty( $available_gateways ) ) {
            remove_action( 'woocommerce_checkout_terms_and_conditions', 'wc_checkout_privacy_policy_text', 20 );
            remove_action( 'woocommerce_checkout_terms_and_conditions', 'wc_terms_and_conditions_page_content', 30 );
        }
}

This is removing Privacy policy and Terms & conditions completely even when there is an available payment gateway.

Ad

Answer

The variable $available_gateways is not defined and woocommerce_checkout_init is the correct hook to be used. Try the following:

add_action('woocommerce_checkout_init', 'disable_checkout_terms_and_conditions', 10 );
function disable_checkout_terms_and_conditions(){
    // Get available payment gateways
    $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
    
    if ( empty( $available_gateways ) ) {
        remove_action( 'woocommerce_checkout_terms_and_conditions', 'wc_checkout_privacy_policy_text', 20 );
        remove_action( 'woocommerce_checkout_terms_and_conditions', 'wc_terms_and_conditions_page_content', 30 );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Related: Remove some hooked functions based on virtual products in WooCommerce Checkout

Ad
source: stackoverflow.com
Ad