2017-08-26 123 views
2

在WooCommerce結帳表單中,我已經自定義了字段。但是,如果產品屬於特定類別,我不希望顯示這些字段。我有那部分工作。在WooCommerce中取消設置有條件的自定義結帳字段

接下來我想取消設置一些自定義結帳字段,但我無法弄清楚如何或現在。清除一個正常的結賬場是容易的,我使用例如下面的代碼:

unset($fields['billing']['billing_company']); 

然而自定義結賬場它不工作。下面是我如何設定這個自定義字段:

function company_details_section($checkout) 
{ 
    woocommerce_form_field('delegate_1_name', array(
     'type' => 'text', 
     'class' => array(
      'my-field-class form-row-wide delegateExists' 
     ) , 
     'label' => __('Full name') , 
    ) , $checkout->get_value('delegate_1_name')); 
} 

由於這屬於自己的部分,而不是計費,運輸或正常WooCommerce附加字段,我找不到刪除的方式。

我曾嘗試以下:

unset($fields['delegate_1_name']); 
unset($fields['additional']['delegate_1_name']); 
unset($fields['billing']['delegate_1_name']); 
unset($fields['shipping']['delegate_1_name']); 
unset($fields['company_details_section']['delegate_1_name']); 

這是我使用的未設置的字段的條件函數:

function wc_ninja_product_is_in_the_cart() { 
    // Add your special product IDs here 
    $ids = array('45', '70', '75');; 

    // Products currently in the cart 
    $cart_ids = array(); 

    // Find each product in the cart and add it to the $cart_ids array 
    foreach(WC()->cart->get_cart() as $cart_item_key => $values) { 
     $cart_product = $values['data']; 
     $cart_ids[] = $cart_product->id; 
    } 

    // If one of the special products are in the cart, return true. 
    if (! empty(array_intersect($ids, $cart_ids))) { 
     return true; 
    } else { 
     return false; 
    } 
} 

這是功能代碼未設置正常結賬領域:

function wc_ninja_remove_checkout_field($fields) { 
    if (! wc_ninja_product_is_in_the_cart()) { 
     unset($fields['billing']['billing_company']); 
    } 

    return $fields; 
} 
add_filter('woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field'); 

如何取消設置WooCommerce中的自定義結帳字段(不是經典的)?

+0

在什麼時候適用於所有版本的WooCommerce你在他們解封? – inarilo

+0

請參閱上面的編輯。 – RD2411

+0

您是否嘗試打印$字段? – inarilo

回答

1

您不能取消設置自定義結帳字段,因爲它沒有像默認的經典WooCommerce結賬字段那樣在數組中設置。

所以你應該直接在自定義字段創建代碼中使用你的條件函數。我也重新審視了一下你的現有代碼:

// Conditional function: If one of the special products is in cart return true, else false 
function is_in_cart() { 
    // Add your special product IDs here 
    $ids = array(45, 70, 75); 

    foreach(WC()->cart->get_cart() as $cart_item){ 
     $product_id = version_compare(WC_VERSION, '3.0', '<') ? $cart_item['data']->id : $cart_item['data']->get_id(); 
     if(in_array($cart_item['data']->get_id(), $ids)) 
      return true; 
    } 
    return false; 
} 

add_filter('woocommerce_checkout_fields' , 'remove_checkout_fields', 10, 1); 
function remove_checkout_fields($fields) { 
    if(! is_in_cart()) 
     unset($fields['billing']['billing_company']); 

    return $fields; 
} 

add_action('woocommerce_after_order_notes', 'company_details_section', 10, 1); 
function company_details_section($checkout){ 
    // Here your conditional function 
    if(is_in_cart()){ 

     echo '<div id="my_custom_checkout_field"><h3>' . __('Company Details') . '</h3>'; 

     woocommerce_form_field('delegate_1_name', array(
      'type' => 'text', 
      'class' => array('my-field-class form-row-wide delegateExists'), 
      'label' => __('Full name') , 
     ) , $checkout->get_value('delegate_1_name')); 

     echo '</div>'; 
    } 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

測試,因爲2.5.X

+0

謝謝,我會試試這個,讓你知道我是怎麼做到的。我在想,用現有的做事方式是不可能的,所以謝謝。 – RD2411

+1

工作很好,對於慢速回復感到抱歉,本站一直在瘋狂忙碌,再次感謝! – RD2411

相關問題