簡體   English   中英

如果購物車 = $0,則 Woocommerce 條件隱藏字段

[英]Woocommerce condition to hide fields if cart = $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;
}

改變

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM