4

I'm currently working on the WooCommerce registration page in my WordPress theme and added a hook for users to accept the Terms & Conditions - see PHP code added.

HTML5 provides a super helpful feature for input fields, which is to add 'required' in the tag and new browser will automatically prompt a warning. I have managed to do this for all other input fields, but I'm unable to amend this specific PHP array to output the <input> tag with the 'required' attribute.

The HTML output would need to look like this:

<input required type="checkbox" class="input-checkbox woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="privacy_policy_reg" id="privacy_policy_reg" value="1">

However, at the moment the produced output based on the PHP code below is:

<input type="checkbox" class="input-checkbox woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="privacy_policy_reg" id="privacy_policy_reg" value="1">

Note: the hook already has a PHP validation check but only after the register button is pressed, while the HTML5 check works instantly. Nevertheless, I want to keep the PHP validation check in case there are browsers that cannot read the HTML5 check.

Could anyone kindly help me how the PHP array code would need to be amended to have the tag to include the 'required' attribute?

I'd very much appreciate any help!

Best, David

// CREATE CHECKBOX AND LABEL

add_action( 'woocommerce_register_form', 'bbloomer_add_registration_privacy_policy', 11 );
   
function bbloomer_add_registration_privacy_policy() {
 
woocommerce_form_field( 'privacy_policy_reg', array(
   'type'          => 'checkbox',
   'class'         => array('form-row privacy'),
   'label_class'   => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
   'input_class'   => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
   'required'      => true,
   'label'         => 'I\'ve read and accept the <a href="/privacy-policy">Privacy Policy</a>',
));
  
}
  
// SHOW ERROR AFTER CLICKING SUBMIT BUTTON IF USER HAS NOT CHECKED CHECKBOX
   
add_filter( 'woocommerce_registration_errors', 'bbloomer_validate_privacy_registration', 10, 3 );
  
function bbloomer_validate_privacy_registration( $errors, $username, $email ) {
if ( ! is_checkout() ) {
    if ( ! (int) isset( $_POST['privacy_policy_reg'] ) ) {
        $errors->add( 'privacy_policy_reg_error', __( 'Privacy Policy consent is required!', 'woocommerce' ) );
    }
}
return $errors;
}

UPDATE

  1. As per other question here: How can i make custom field value required ( compulsory ) in woocommerce product page when adding product I have already (unsuccessfully) tried:
'custom_attributes' => array( 'required' => 'required' ),
  1. As per suggestions, I have already tried:
required => "required"

required => "required"
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
AlphaX
  • 615
  • 1
  • 11
  • 25

1 Answers1

4

If you actually want to change <input.. to <input required.. then you can add the woocommerce_form_field_checkbox filter hook.

The fact is that where the HTML code is created

(This code was copied from wc-template-functions.php line 2799)

<input type="' . esc_attr( $args['type'] ) . '" class="input-checkbox ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="1" ' . checked( $value, 1, false ) . ' /> ' . $args['label'] . $required . '</label>';

the option exists to add arguments to the existing $args but not extra HTML code to the HTML itself


So if you add the code below to your existing code, you have the option to adjust the HTML output from the $field

  • str_replace - Replace all occurrences of the search string with the replacement string
function filter_woocommerce_form_field_checkbox( $field, $key, $args, $value ) {
    // Based on key
    if ( $key == 'privacy_policy_reg' ) {
        $field = str_replace( '<input', '<input required', $field );
    }
    
    return $field;
}
add_filter( 'woocommerce_form_field_checkbox', 'filter_woocommerce_form_field_checkbox', 10, 4 );

Related: How should I change the woocommerce_form_field HTML Structure?

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • First of all: THANKS A LOT! This works perfectly! I have to admit I'm still a newbie on woocommerce development and while I understand the first part of your answer, that the HTML of cannot be edited directly via the array, I struggle to understand the second part with the filter. Of course I understand the code but not how the filter and $field is related back to the tag? The doesn't have any $field attribute. I'm just slightly confused here how this is related with eachother. I'd truly appreciate your explanation. Thanks again for the solution!! – AlphaX Oct 04 '20 at 13:13
  • 1
    Basically it means that you can rewrite the entire code that contains the [woocommerce_form_field function](https://github.com/woocommerce/woocommerce/blob/d7b2207e75eedb4fb89249b81dcd42133e8ca616/includes/wc-template-functions.php#L2662-L2894) via the filter hook (the filters can be found at the bottom of this function). As you will see, this function contains code for all types of fields (select, textarea, radio, checkboc, etc..) In my [answer](https://stackoverflow.com/a/63999062/11987538) to another question you can find more information about how this works. – 7uc1f3r Oct 04 '20 at 13:33