Add WooCommerce dynamic country and state dropdowns to login or register forms.

huangapple go评论77阅读模式
英文:

Add WooCommerce dynamic country and state dropdowns to login or register forms

问题

我正在使用WooCommerce网站,并使用digits OTP登录和注册插件。我需要在digits注册表单上添加国家和州的下拉框。在结账页面上,对于账单详情,下拉框正常工作。但在我的注册页面上不起作用。我该如何修复呢?

这是我的代码:

global $woocommerce;
$countries_obj = new WC_Countries();
$countries = $countries_obj->get_countries();
echo '<div id="my_custom_countries_field"><h2>' . __('Countries') . '</h2>';

woocommerce_form_field('my_country_field', array(
    'type' => 'select',
    'class' => array('chzn-drop'),
    'label' => __('Select a country'),
    'placeholder' => __('Enter something'),
    'options' => $countries
));

echo '</div>';

这是我在WPCode插件上尝试的代码。但对我不起作用。我想在这个第三方插件的注册表单上展示国家和州的下拉框。

非常感谢任何帮助。

英文:

I'm using WooCommerce website, with digits plugin for OTP login and sign up. I need country and state field dropdowns on digits sign up form. On the checkout page, for billing details, the dropdown is working well. But not on my sign-up page. How can I fix that?

This is my code:

global $woocommerce;
    $countries_obj   = new WC_Countries();
    $countries   = $countries_obj-&gt;__get(&#39;countries&#39;);
    echo &#39;&lt;div id=&quot;my_custom_countries_field&quot;&gt;&lt;h2&gt;&#39; . __(&#39;Countries&#39;) . &#39;&lt;/h2&gt;&#39;;

    woocommerce_form_field(&#39;my_country_field&#39;, array(
    &#39;type&#39;       =&gt; &#39;select&#39;,
    &#39;class&#39;      =&gt; array( &#39;chzn-drop&#39; ),
    &#39;label&#39;      =&gt; __(&#39;Select a country&#39;),
    &#39;placeholder&#39;    =&gt; __(&#39;Enter something&#39;),
    &#39;options&#39;    =&gt; $countries
    )
    );
    echo &#39;&lt;/div&gt;&#39;;

This is the code I tried on the WPCode plugin. But I didn't work for me. I want to showcase the drop-down for country and state on this third party plugin signup form.

Any help is highly appreciated.

答案1

得分: 1

首先,woocommerce_form_field() 函数 提供了 "country""state" 字段类型,因此请使用它们而不是 "select" 类型。这样,您无需获取国家选项或州选项。此外,在这里,只有处理州的国家才会显示州字段,并根据所选国家填充正确的州。

Digits:WordPress移动电话号码OTP注册和登录表单的评论中,我已经提出了这个问题,因为这个商业插件似乎支持WooCommerce。您可以作为购买者提交支持工单来询问他们。

以下基于此前的答案的代码将与任何登录和注册表单无缝配合使用(您只需要替换功能挂钩,或者选择函数内的代码,并将其添加到所需的登录/注册模板文件中):

add_action('woocommerce_login_form', 'add_country_and_state_to_login_form');
function add_country_and_state_to_login_form() {
    global $current_user;

    wp_enqueue_script('wc-country-select');
    wp_enqueue_script('wc-state-select');

    woocommerce_form_field('billing_country', array(
        'type'        => 'country',
        'class'       => array('chzn-drop'),
        'label'       => __('Select a country'),
        'placeholder' => __('Enter something'),
        'required'    => true,
        'clear'       => true,
        'default'     => '',
    ), isset($current_user->billing_country) ? $current_user->billing_country : '');

    woocommerce_form_field('billing_state', array(
        'type'        => 'state',
        'class'       => array('chzn-drop'),
        'label'       => __('Select a state'),
        'placeholder' => __('Enter something'),
        'required'    => true,
        'clear'       => true,
    ), isset($current_user->billing_state) ? $current_user->billing_state : '');
}

提醒:
州字段仅在WooCommerce处理州的国家中显示。


附加内容:

对于其他想要在WooCommerce登录表单中使用该代码的用户,以下是验证和保存提交的国家和州所需的代码:

// 验证
add_action('woocommerce_register_post', 'country_and_state_login_validation', 20, 3);
function country_and_state_login_validation($username, $email, $validation_errors) {
    $domain = 'woocommerce';
    $error  = '<strong>' . __('Error', $domain) . '</strong>: ';

    if (isset($_POST['billing_country']) && empty($_POST['billing_country'])) {
        $validation_errors->add('billing_country_error', $error . __('Country is required!.', $domain));
    }

    if (isset($_POST['billing_state']) && empty($_POST['billing_state'])) {
        $validation_errors->add('billing_state_error', $error . __('State is required!.', $domain));
    }

    return $validation_errors;
}

// 保存
add_action('woocommerce_created_customer', 'save_login_country_and_state', 20, 3);
function save_login_country_and_state($customer_id, $new_customer_data, $password_generated) {
    $customer = new WC_Customer($customer_id);
    $do_save  = false;

    if (isset($_POST['billing_country']) && !empty($_POST['billing_country'])) {
        $customer->update_meta_data('billing_country', esc_attr($_POST['billing_country']));
        $do_save = true;
    }

    if (isset($_POST['billing_state']) && !empty($_POST['billing_state'])) {
        $customer->update_meta_data('billing_state', esc_attr($_POST['billing_state']));
        $do_save = true;
    }

    if ($do_save) {
        $customer->save();
    }
}

将代码放在您的活动子主题(或主题)的functions.php文件中,或者放在任何插件文件中。
经过测试,可正常工作。

英文:

First "country" and "state" field types are available for woocommerce_form_field() function, so use them instead of the "select" type. This way, you don't need to get the countries options or states options. Additionally, here, the state field will be visible only for countries handling states, and populated with the correct states for the selected country.

I have asked in Digits : WordPress Mobile Phone Number OTP Signup and Login Form as a comment, as this commercial plugin seems to support WooCommerce. You may ask them, as a purchaser, opening a support ticket.

The following code, based on this previous answer, will work flawlessly with any login and sign up form (you will just need to replace the function hook, or to pick the code inside the function, and add it to the required login / register template file):

add_action( &#39;woocommerce_login_form&#39;, &#39;add_country_and_state_to_login_form&#39; );
function add_country_and_state_to_login_form() {
    global $current_user;
  
    wp_enqueue_script(&#39;wc-country-select&#39;);
    wp_enqueue_script(&#39;wc-state-select&#39;);

    woocommerce_form_field(&#39;billing_country&#39;, array(
        &#39;type&#39;        =&gt; &#39;country&#39;,
        &#39;class&#39;       =&gt; array(&#39;chzn-drop&#39;),
        &#39;label&#39;       =&gt; __(&#39;Select a country&#39;),
        &#39;placeholder&#39; =&gt; __(&#39;Enter something&#39;),
        &#39;required&#39;    =&gt; true,
        &#39;clear&#39;       =&gt; true,
        &#39;default&#39;     =&gt; &#39;&#39;,
    ), isset($current_user-&gt;billing_country) ? $current_user-&gt;billing_country : &#39;&#39; );

    woocommerce_form_field(&#39;billing_state&#39;, array(
        &#39;type&#39;        =&gt; &#39;state&#39;,
        &#39;class&#39;       =&gt; array(&#39;chzn-drop&#39;),
        &#39;label&#39;       =&gt; __(&#39;Select a state&#39;),
        &#39;placeholder&#39; =&gt; __(&#39;Enter something&#39;),
        &#39;required&#39;    =&gt; true,
        &#39;clear&#39;       =&gt; true,
    ), isset($current_user-&gt;billing_state) ? $current_user-&gt;billing_state : &#39;&#39; );
}

> Reminder: <br>The state field will be only displayed for countries handling states in WooCommerce.


Addition:

For those other fellows, who want to use that code in WooCommerce login form, here is the necessary code to validate and save the submitted country and state:

// Validation
add_action( &#39;woocommerce_register_post&#39;, &#39;country_and_state_login_validation&#39;, 20, 3 );
function country_and_state_to_login_validation( $username, $email, $validation_errors ) {
    $domain = &#39;woocommerce&#39;;
    $error  = &#39;&lt;strong&gt;&#39; . __( &#39;Error&#39;, $domain ) . &#39;&lt;/strong&gt;: &#39;;
    
    if ( isset($_POST[&#39;billing_country&#39;]) &amp;&amp; empty( $_POST[&#39;billing_country&#39;] ) ) {
        $validation_errors-&gt;add( &#39;billing_country_error&#39;, $error . __( &#39;Country is required!.&#39;, $domain ) );
    }
    
    if ( isset($_POST[&#39;billing_state&#39;]) &amp;&amp; empty( $_POST[&#39;billing_state&#39;] ) ) {
        $validation_errors-&gt;add( &#39;billing_state_error&#39;, $error . __( &#39;State is required!.&#39;, $domain ) );
    }

    return $validation_errors;
}

// Save 
add_action(&#39;woocommerce_created_customer&#39;, &#39;save_login_country_and_state&#39;, 20, 3 );
function save_login_country_and_state( $customer_id, $new_customer_data, $password_generated ) {
    $customer = new WC_Customer($customer_id);
    $do_save  = false;

    if (isset($_POST[&#39;billing_country&#39;]) &amp;&amp; !empty($_POST[&#39;billing_country&#39;])) {
        $customer-&gt;update_meta_data(&#39;billing_country&#39;, esc_attr($_POST[&#39;billing_country&#39;]));
        $do_save = true;
    }

    if (isset($_POST[&#39;billing_state&#39;]) &amp;&amp; !empty($_POST[&#39;billing_state&#39;])) {
        $customer-&gt;update_meta_data(&#39;billing_state&#39;, esc_attr($_POST[&#39;billing_state&#39;]));
        $do_save = true;
    }

    if ($do_save) {
        $customer-&gt;save();
    }
}

Code goes on functions.php file of your active child theme (or theme), or in any plugin file. <br>
Tested and works.

答案2

得分: 0

为了解决在您的Digits注册表单上看不到国家下拉菜单的问题,您可以尝试以下步骤:

确保您已在您的WooCommerce网站上安装并激活了Digits插件。

  1. 定位包含您的Digits注册表单定义的文件或模板。这可能会根据您的主题或自定义而有所不同。
  2. 在该文件中,找到正在呈现表单字段的部分,特别是国家字段。

将国家字段的现有代码替换为以下代码:

global $woocommerce;
$countries_obj = new WC_Countries();
$countries = $countries_obj->get_countries();
echo '<div id="my_custom_countries_field"><h2>' . __('Countries') . '</h2>';

woocommerce_form_field('my_country_field', array(
    'type' => 'select',
    'class' => array('chzn-drop'),
    'label' => __('Select a country'),
    'placeholder' => __('Enter something'),
    'options' => $countries
));

echo '</div>';

通过使用$countries_obj->get_countries()函数而不是$countries_obj->__get('countries'),您可以以与Digits和最新版本的WooCommerce兼容的方式检索国家选项。

请注意,确切的位置和文件名可能会根据您的特定设置和主题定制而有所不同。如果您对要修改的特定文件不确定,可能需要查阅您的主题文档或联系主题开发人员以获取进一步的帮助。

英文:

To fix the issue with the country dropdown not appearing on your Digits sign-up form, you can try the following steps:

Make sure that you have the Digits plugin installed and activated on your WooCommerce website.

  1. Locate the file or template where your Digits sign-up form is
    defined. This can vary depending on your theme or customization.
  2. In that file, find the section where the form fields are being
    rendered, specifically the country field.

Replace the existing code for the country field with the following code:

global $woocommerce;
$countries_obj = new WC_Countries();
$countries = $countries_obj-&gt;get_countries();
echo &#39;&lt;div id=&quot;my_custom_countries_field&quot;&gt;&lt;h2&gt;&#39; . __(&#39;Countries&#39;) . &#39;&lt;/h2&gt;&#39;;

woocommerce_form_field(&#39;my_country_field&#39;, array(
    &#39;type&#39; =&gt; &#39;select&#39;,
    &#39;class&#39; =&gt; array(&#39;chzn-drop&#39;),
    &#39;label&#39; =&gt; __(&#39;Select a country&#39;),
    &#39;placeholder&#39; =&gt; __(&#39;Enter something&#39;),
    &#39;options&#39; =&gt; $countries
));

echo &#39;&lt;/div&gt;&#39;;

By using the $countries_obj-&gt;get_countries() function instead of $countries_obj-&gt;__get(&#39;countries&#39;), you can retrieve the country options in a way that is compatible with Digits and the latest version of WooCommerce.

Please note that the exact location and file names may vary based on your specific setup and theme customization. You may need to consult your theme documentation or contact the theme developer for further assistance if you're unsure about the specific file to modify.

huangapple
  • 本文由 发表于 2023年7月17日 15:24:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定