WooCommerce Checkout:如果填充另一个&#x2B,则如何根据需要设置字段;如何使用来自另一个字段的数据自动填充字段

发布于 2025-01-20 08:21:53 字数 786 浏览 2 评论 0原文

我正在寻找解决问题的解决方案。第一个问题是,如果检查“购买为公司”,我需要根据需要制作现场公司ID。公司复选框的ID是“ Wi_as_company”。我正在尝试将其像Code Bellow一样,但它总是根据需要(也适用于非公司客户)字段“ Billing_company_wi_id”。

add_filter( 'woocommerce_checkout_fields' , 'company_checkbox_and_new_checkout_fields_1', 9999 );
function company_checkbox_and_new_checkout_fields_1( $fields ) {
        if (isset($_POST['wi_as_company'])) {          
            $fields['billing']['billing_company_wi_id']['required'] = false;
            } else {
            $fields['billing']['billing_company_wi_id']['required'] = true;  
        }
return $fields;
}

我的第二个问题是我想自动将数据(前8个数字)从一个字段移到另一个字段,然后在之前添加2个字母。一个字段具有这种格式:

12345678-Y-YY

我想将前8个字符移至这样的另一个字段:

XX12345678

我将非常感谢任何建议。

I am looking for solution for my problems in checkout. First problem is that I need to make field company ID as required if field "Buy as company" is checked. ID of company checkbox is "wi_as_company". I am try to make it like code bellow, but it makes field "billing_company_wi_id" always as required (also for non-company customers).

add_filter( 'woocommerce_checkout_fields' , 'company_checkbox_and_new_checkout_fields_1', 9999 );
function company_checkbox_and_new_checkout_fields_1( $fields ) {
        if (isset($_POST['wi_as_company'])) {          
            $fields['billing']['billing_company_wi_id']['required'] = false;
            } else {
            $fields['billing']['billing_company_wi_id']['required'] = true;  
        }
return $fields;
}

My second problem is that I want to move data (first 8 numbers) automatically from one field to another one and add 2 letters before. One field have this format:

12345678-Y-YY

and I want to move first 8 characters to another field like this:

XX12345678

I will be very grateful for any suggestions.

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

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

发布评论

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

评论(2

时光沙漏 2025-01-27 08:21:53

第一个问题的答案:如果选中复选框,则需要字段公司 ID。将以下代码写入您的子主题functions.php文件并更改元素的ID:
wi_as_company 是复选框的 ID 并且
如果选中复选框,billing_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" );     
    }
 }

ANSWER FOR FIRST PROBLEM: If checkbox is checked, then field company ID is required. Write code bellow to your child themes functions.php file and change ID of elements:
wi_as_company is ID of checkbox and
billing_company_wi_id is ID of required field if checkbox is checked

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" );     
    }
 }
泪痕残 2025-01-27 08:21:53

第二个问题的答案:更改字段的代码ID和要复制的字符数。在这种情况下,它将从“ billing_company_wi_tax”中复制前8个字符为“ billing_company_wi_vat”,然后在复制文本之前添加字母“ xx”。将此功能插入您的孩子主题functions.php。

add_action( 'wp_footer', 'copy_field', 9999 );
function copy_field() {
   global $wp;
   if ( is_checkout() ) {
      echo '<script> document.getElementById("billing_company_wi_tax").setAttribute("onkeyup","URLChange(this.value);");
        function URLChange(titlestr) {
        var url=titlestr.replace(/ /g,"-");
        url = url.substring(0, 8);
        document.getElementsByName("billing_company_wi_vat")[0].value="XX"+url;
      }</script>';
   }
}

ANSWER FOR SECOND PROBLEM: Change in code ID of fields and number of characters to copy. In this case it will copy first 8 characters from "billing_company_wi_tax" to "billing_company_wi_vat" plus it will add letters "XX" before copied text. Insert this function to your child themes functions.php.

add_action( 'wp_footer', 'copy_field', 9999 );
function copy_field() {
   global $wp;
   if ( is_checkout() ) {
      echo '<script> document.getElementById("billing_company_wi_tax").setAttribute("onkeyup","URLChange(this.value);");
        function URLChange(titlestr) {
        var url=titlestr.replace(/ /g,"-");
        url = url.substring(0, 8);
        document.getElementsByName("billing_company_wi_vat")[0].value="XX"+url;
      }</script>';
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文