如果购物车= $ 0

发布于 2025-02-11 16:49:05 字数 1196 浏览 2 评论 0原文

我正在尝试确保某些计费信息字段是否为虚拟产品,而购物车为$ 0。我在第二种情况下遇到了麻烦。我已经尝试使用Stackoverflow上其他帖子的条件,但是遇到了一个奇怪的问题,如果购物车= 0,则删除了所有字段

add_filter( 'woocommerce_checkout_fields' , 'bbloomer_simplify_checkout_virtual' );
 
function bbloomer_simplify_checkout_virtual( $fields ) {
   $only_virtual = true;
   
   global $woocommerce;
    if ($woocommerce->cart->get_cart_total() != 0 ) {
    return;
    }
    
   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 ) {
       unset($fields['billing']['billing_company']);
       unset($fields['billing']['billing_address_1']);
       unset($fields['billing']['billing_address_2']);
       unset($fields['billing']['billing_city']);
       unset($fields['billing']['billing_postcode']);
       unset($fields['billing']['billing_country']);
       unset($fields['billing']['billing_state']);
       unset($fields['billing']['billing_phone']);
       add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
     }
     
     return $fields;
}

I'm trying to make sure that certain billing information fields are unset if the product is virtual and the cart amounts to $0. I'm having trouble with the second condition. I've tried using conditions from other posts on stackoverflow, but run into a weird issue where all fields are removed if the cart != 0

add_filter( 'woocommerce_checkout_fields' , 'bbloomer_simplify_checkout_virtual' );
 
function bbloomer_simplify_checkout_virtual( $fields ) {
   $only_virtual = true;
   
   global $woocommerce;
    if ($woocommerce->cart->get_cart_total() != 0 ) {
    return;
    }
    
   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 ) {
       unset($fields['billing']['billing_company']);
       unset($fields['billing']['billing_address_1']);
       unset($fields['billing']['billing_address_2']);
       unset($fields['billing']['billing_city']);
       unset($fields['billing']['billing_postcode']);
       unset($fields['billing']['billing_country']);
       unset($fields['billing']['billing_state']);
       unset($fields['billing']['billing_phone']);
       add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
     }
     
     return $fields;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亢潮 2025-02-18 16:49:05

更改

if ($woocommerce->cart->get_cart_total() != 0 ) {
    return;
}

if ($woocommerce->cart->get_cart_total() != 0 ) {
    return $fields;
}

Change

if ($woocommerce->cart->get_cart_total() != 0 ) {
    return;
}

To

if ($woocommerce->cart->get_cart_total() != 0 ) {
    return $fields;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文