在WooCommerce中取消设置有条件的自定义结帐字段

在WooCommerce中取消设置有条件的自定义结帐字段

问题描述:

在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

您不能取消设置自定义结帐字段,因为它没有像默认的经典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