Remove some hooked functions based on virtual products in WooCommerce Checkout

允我心安 提交于 2021-01-28 02:00:56

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/63176539/remove-some-hooked-functions-based-on-virtual-products-in-woocommerce-checkout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!