Why are my 'min' and 'max' form functions not working?

社会主义新天地 提交于 2021-02-11 13:45:01

问题


I made a custom form to display in Wordpress Woocommerce checkout page, where user must select their age before making the purchase.

The idea is so that people who are under 18 years old would not be able to make a purchase.

Currently, the code displays the form and it is all set, but 'min' and 'max' features, which I would require to limit age insertion between 18 and 99, do not apply for some reason. What do I require in order to make 'min' and 'max' functions to apply?

add_action('woocommerce_after_order_notes', 'custom_checkout_field');

function custom_checkout_field($checkout)
{
    echo '<div id="custom_checkout_field"><h3>' . __('Vanusepiirang') . '</h3>';

    woocommerce_form_field('custom_field_name', array(
        'type'      => 'number',
        'id'        => 'vanusepiirang',
        'name'      => 'vanusepiirang',
        'min'       =>  18,
        'max'       =>  99,
        'required'  =>  1,
        'class'     => array(
            'my-field-class form-row-wide'
        ),
        'label'     => __('Sisestage enda vanus. Teie sünnipäeva küsime kinnituseks, et olete piisavalt vana ostukorvis olevate toodete ostmiseks.') ,
        'placeholder' => __('Teie vanus') ,
    ),

    $checkout->get_value('custom_field_name'));
    echo '</div>';
}

I expect 'min' and 'max' features to apply on the form, so that numbers 1-17 cannot be passed through.


回答1:


According to the woocommerce_form_field function definition, max and min attributes must be passed as custom_attribute. Your final code must be modified as below:

add_action('woocommerce_after_order_notes', 'custom_checkout_field');

function custom_checkout_field($checkout)
{
    echo '<div id="custom_checkout_field"><h3>' . __('Vanusepiirang') . '</h3>';

    woocommerce_form_field('custom_field_name', array(
        'type'      => 'number',
        'id'        => 'vanusepiirang',
        'name'      => 'vanusepiirang',
        'custom_attributes' => array(
            'min'       =>  18,
            'max'       =>  99,
        ),
        'required'  =>  1,
        'class'     => array(
            'my-field-class form-row-wide'
        ),
        'label'     => __('Sisestage enda vanus. Teie sünnipäeva küsime kinnituseks, et olete piisavalt vana ostukorvis olevate toodete ostmiseks.') ,
        'placeholder' => __('Teie vanus') ,
    ),

    $checkout->get_value('custom_field_name'));
    echo '</div>';
}

Also you should consider that, first argument of the woocommerce_form_field function is the field name and ID, unless you pass an ID parameter separately in the second argument. Which means, because of the 'name' => 'vanusepiirang' key-value generated input html will contain two different name attribute. I recommend to remove 'name' => 'vanusepiirang' part from your source code.




回答2:


Sort of helped myself out with a different kind of solution :)

Added this to my 'empty field check function':

else if ($_POST['custom_field_name'] < ('18'))
    wc_add_notice(__('Te pole piisavalt vana nende toodete ostmiseks!') , 'error');
else if ($_POST['custom_field_name'] > ('99'))
    wc_add_notice(__('Palun sisesta korrektne vanus!') , 'error');


来源:https://stackoverflow.com/questions/54279129/why-are-my-min-and-max-form-functions-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!