Alex Banks
Alex Banks

Reputation: 67

Make AutomateWoo checkbox required on WooCommerce registration

The AutomateWoo plugin adds a checkbox field on the registration section (located on My Account page when user is not logged in) and I am trying here to make it required.

I don't want people to be able to register without ticking this box.

The box is an optin and displays as follows:

<p class="automatewoo-optin form-row">
    <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
        <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="automatewoo_optin" id="automatewoo_optin">
        <span class="automatewoo-optin__checkbox-text">I want to receive updates about products and promotions.</span>
    </label>
</p>

Here is my code attempt to make this checkbox "required":

add_filter( 'woocommerce_register_form_start', 'bd_require_automatewoo_optin');
function bd_require_automatewoo_optin( $fields ) {
$fields['automatewoo_optin']['required'] = true;
return $fields;
}

I have also tried using woocommerce_register_form and woocommerce_forms_field hooks.

Image preview of the "Register" form, checkbox is located at the bottom.

Checkbox added to the bottom of the register form, must be made required.

How can I make Make AutomateWoo checkbox required on WooCommerce registration section?


Edit:

In some cases, you may need to replace the second 'required' with 'true'…

<script>
jQuery(document).ready(function($){
   $('input[name="automatewoo_optin"]').prop('required', 'required');
});
</script>

Upvotes: 1

Views: 707

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254363

To make your checkbox field located on WooCommerce registration form required, use the following:

// Validate WooCommerce registration form custom fields.
add_action( 'woocommerce_register_post', 'wc_validate_automatewoo_optin_fields', 10, 3 );
function wc_validate_automatewoo_optin_fields( $username, $email, $validation_errors ) {
    if ( ! isset($_POST['automatewoo_optin']) || empty($_POST['automatewoo_optin']) ) {
        $validation_errors->add('optin_error', __('The Checkbox is a required field', 'woocommerce') );
    }
        
    return $validation_errors;
}

Then optionally use also the following to make your checkbox field visually required:

// Mark the checkbox field as required
add_filter( 'woocommerce_register_form_end', 'add_automatewoo_optin_jquery');
function add_automatewoo_optin_jquery() {
    ?>
    <script>
    (function($){
       $('p.automatewoo-optin').addClass('validate-required');
       $('p.automatewoo-optin > label > span').append('<abbr class="required" title="required">*</abbr>');
    })(jQuery);
    </script>
    <?php
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Upvotes: 1

Howard E
Howard E

Reputation: 5669

Use jQuery. Either add to your theme scripts, or you can add to wp_footer and wrap in <script> </script>

jQuery(document).ready(function($){
   $('input[name="automatewoo_optin"]').prop('checked', true);
});

Upvotes: 0

Related Questions