删除 WooCommerce Checkout 中一些基于虚拟产品的挂钩功能

当购物车中只有虚拟产品时,我想删除结账页面上的一些信息。


以下是删除我想要在结帐页面上显示的内容:


  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 );

当购物车中只有虚拟产品时如何制作相同的产品?


我尝试了以下方法:


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;

}

但这不起作用。有什么建议么?


皈依舞
浏览 96回答 1
1回答

猛跑小猪

更新 2 - 在这种情况下使用的正确钩子是woocommerce_checkout_init操作钩子:// Custom conditional function that checks if there is only virtual items in cartfunction has_only_virtual_items_in_cart(){&nbsp; &nbsp; $only_virtual = true;&nbsp; &nbsp; // Check if there are non-virtual items in cart&nbsp; &nbsp; foreach( WC()->cart->get_cart() as $cart_item ) {&nbsp; &nbsp; &nbsp; &nbsp; if ( ! $cart_item['data']->is_virtual() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $only_virtual = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $only_virtual;}// Unhook some functions conditionallyadd_action( 'woocommerce_checkout_init', 'simplify_checkout_for_virtual_items_only' );function simplify_checkout_for_virtual_items_only() {&nbsp; &nbsp; if( has_only_virtual_items_in_cart() ) {&nbsp; &nbsp; &nbsp; &nbsp; remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );&nbsp; &nbsp; &nbsp; &nbsp; remove_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );&nbsp; &nbsp; &nbsp; &nbsp; remove_action( 'woocommerce_checkout_terms_and_conditions', 'wc_checkout_privacy_policy_text', 20 );&nbsp; &nbsp; &nbsp; &nbsp; remove_action( 'woocommerce_checkout_terms_and_conditions', 'wc_terms_and_conditions_page_content', 30 );&nbsp; &nbsp; &nbsp; &nbsp; remove_action( 'woocommerce_checkout_terms_and_conditions', 'woocontracts_terms_fields' ); // <== missing priority&nbsp; &nbsp; &nbsp; &nbsp; remove_action( 'woocommerce_checkout_after_terms_and_conditions', 'woocontracts_checkout_additional_checkboxes', 10 );&nbsp; &nbsp; }}代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。然后,您可以在挂钩中的函数has_only_virtual_items_in_cart()内重用自定义条件函数。woocontracts_checkout_field_process()woocommerce_checkout_process
打开App,查看更多内容
随时随地看视频慕课网APP