【问题标题】:Woocommerce Checkout - Add conditional field required if one field is filledWoocommerce Checkout - 如果填写了一个字段,则需要添加条件字段
【发布时间】:2022-04-24 04:32:05
【问题描述】:

我想在结帐页面上添加条件要求。因为很多人填写公司名称而不是公司。我想在“billing_company”字段中添加一个条件,如果客户填写“billing_company”字段也需要填写“woocommerce_eu_vat_number”字段。问题是这两个字段是可选的。但如果 billing_company /billing_vat_number 已填满,我想按要求进行操作。有可能吗?

HTML 中的字段代码:

<p class="form-row form-row-wide" id="billing_company_field" data-priority="30">
   <label for="billing_company" class="">Company name&nbsp;<span class="optional">(optional)</span></label>
   <span class="woocommerce-input-wrapper czr-focus">
      <input type="text" class="input-text czr-focusable" name="billing_company" id="billing_company" placeholder="" value="" autocomplete="organization">
   </span>
</p>
<p class="form-row form-row-wide" id="woocommerce_eu_vat_number_field" data-priority="120">
   <label for="woocommerce_eu_vat_number" class="">VAT number (companies)&nbsp;<span class="optional">(optional)</span></label>
   <span class="woocommerce-input-wrapper czr-focus">
      <input type="text" class="input-text czr-focusable" name="billing_vat_number" id="woocommerce_eu_vat_number" placeholder="" value="">
   </span>
</p>

我想要这样的代码。对不起,因为我可以理解或多或少的 php 代码,但我不知道如何从零开始创建它。 T_T

<?php

function custom_required_vat_billing_fields() {

if( !empty ('billing_company' ) {

        set ($fields['billing']['billing_vat_number'] = class [REQUIRED=true] );)

} if not {

    wc_add_notice(__('Please enter a VAT number if you are a company'), 'error');
}

};

    return $fields;

}
add_filter('woocommerce_checkout_fields','custom_required_vat_billing_fields');


?>

【问题讨论】:

    标签: php wordpress woocommerce conditional-statements


    【解决方案1】:

    如果选中复选框,则需要填写公司 ID。将以下代码写入您的子主题 functions.php 文件并更改元素的 ID: wi_as_company 是复选框的 IDbilling_company_wi_id 是必填字段的 ID(如果选中复选框)

    add_action( 'woocommerce_checkout_process', 'afm_validation' ); 
    function afm_validation() {     
        if ( isset($_POST['wi_as_company']) && isset($_POST['billing_company_wi_id']) && empty($_POST['billing_company_wi_id']) ) {         
            wc_add_notice( __("Please fill company ID"), "error" );     
        }
     }
    

    【讨论】:

      猜你喜欢
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      相关资源
      最近更新 更多