简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM