MainWP Community

Simplify checkout for virtual products if using WooCommerce

Simplify checkout for virtual products if using WooCommerce on your child site.

Snippet Type

Execute on Child Sites

Snippet

function has_only_virtual_items_in_cart(){
    $only_virtual = true;

    // Check if there are non-virtual items in cart
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if ( ! $cart_item['data']->is_virtual() ) {
            $only_virtual = false;
            break;
        }
    }
    return $only_virtual;
}

// Unhook some functions conditionally
add_action( 'woocommerce_checkout_init', 'simplify_checkout_for_virtual_items_only' );
function simplify_checkout_for_virtual_items_only() {
    if( has_only_virtual_items_in_cart() ) {
        remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
        remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
        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 );
        remove_action( 'woocommerce_checkout_terms_and_conditions', 'woocontracts_terms_fields' ); // <== missing priority
        remove_action( 'woocommerce_checkout_after_terms_and_conditions', 'woocontracts_checkout_additional_checkboxes', 10 );
    }
}
1 Like