Ad

Remove Some Hooked Functions Based On Virtual Products In WooCommerce Checkout

I want to remove some information on checkout page when there is only virtual products in cart.

The following is removing what I want on checkout page:

  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', );
  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_after_terms_and_conditions', 'woocontracts_checkout_additional_checkboxes', 10 );
  remove_action( 'woocommerce_checkout_process', 'woocontracts_checkout_field_process', 10 );

how to make the same when there is only virtual products in cart?

I Tried the following:

add_filter( 'woocommerce_checkout_after_terms_and_conditions' , 'bbloomer_simplify_checkout_virtualab' );
 
function bbloomer_simplify_checkout_virtualab( $fields ) {
    
   $only_virtual = true;
    
   foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
      // Check if there are non-virtual products
      if ( ! $cart_item['data']->is_virtual() ) $only_virtual = false;   
   }
     
    if( $only_virtual ) {
                

  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', );
  remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
  remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
        
return $fields;
}
     
     return $fields;
}

But it doesn't work. Any suggestions?

Ad

Answer

Update 2 - The right hook to use in this case is woocommerce_checkout_init action hook:

// Custom conditional function that checks if there is only virtual items in cart
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 );
    }
}

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

Then you can reuse the custom contional function has_only_virtual_items_in_cart() inside woocontracts_checkout_field_process() function hooked in woocommerce_checkout_process hook.

Ad
source: stackoverflow.com
Ad