Ryan
Ryan

Reputation: 642

How can i disable a checkbox field in woocommerce?

I have added a checkbox field in woocommerce and I want to only view its result on the accounts screen. I have added the checkbox using an example like:

            'register-checkboxes' => array(
            'type'     => 'checkboxes',
            'label'    => __( 'Regions of Construction', 'woo' ),
            'options'  => array(
                'qld1' => __( 'QLD Zone Q1', 'woo' ),
                'qld2' => __( 'QLD Zone Q2', 'woo' ),
                'qld3' => __( 'QLD Zone Q3', 'woo' ),
            ),
            'hide_in_registration' => true,
            'required' => true,
        ),

But when I add:

$field_args['custom_attributes'] = array( 'disabled' => 'disabled' );

On the Account screen this works for disabling Text inputs and selects but not checkbox options.

I am using: https://rudrastyh.com/woocommerce/woocommerce_form_field.html as a guide but it seems you can't pass an array of Custom Attributes to each option of the checkboxes? What am I missing?

Upvotes: 0

Views: 1944

Answers (3)

Ryan
Ryan

Reputation: 642

I added:

add_filter( 'woocommerce_form_field_checkboxes', array( $this,'pm_form_field_modify' ), 10, 4);

then

    public function pm_form_field_modify( $field, $key, $args, $value ) {
    ob_start();
    $this->pm_print_list_field( $key, $args, $value );
    $field = ob_get_clean();

    if ( $args['return'] ) {
        return $field;
    } else {
        echo $field;
    }
}

Then the output

    public function pm_print_list_field( $key, $field_args, $value = null ) {
    $value = empty( $value ) && $field_args['type'] === 'checkboxes' ? array() : $value;
    ?>
    <div class="form-row">
        <?php 

        // Disable the checkboxes for Zones
        if ($field_args['id'] == 'register-checkboxes' && !is_admin()) {
            $field_args['custom_attributes'] = array( 'disabled' => 'disabled' );
        }


        if ( ! empty( $field_args['label'] ) ) { ?>
            <label>
                <?php echo $field_args['label']; ?>
                <?php if ( ! empty( $field_args['required'] ) ) { ?>
                    <abbr class="required" title="<?php echo esc_attr__( 'required', 'woocommerce' ); ?>">*</abbr>
                <?php } ?>
            </label>
        <?php } ?>
        <ul>
            <?php foreach ( $field_args['options'] as $option_value => $option_label ) {
                $id         = sprintf( '%s_%s', $key, sanitize_title_with_dashes( $option_label ) );
                $option_key = $field_args['type'] === 'checkboxes' ? sprintf( '%s[%s]', $key, $option_value ) : $key;
                $type       = $field_args['type'] === 'checkboxes' ? 'checkbox' : $field_args['type'];
                $checked    = $field_args['type'] === 'checkboxes' ? in_array( $option_value, $value ) : $option_value == $value;
                ?>
                <li>
                    <label for="<?php echo esc_attr( $id ); ?>">
                        <input type="<?php echo esc_attr( $type ); ?>" id="<?php echo esc_attr( $id ); ?>" name="<?php echo esc_attr( $option_key ); ?>" value="<?php echo esc_attr( $option_value ); ?>" <?php checked( $checked ); 

                         // Disable the checkboxes
                         if ( isset($field_args['custom_attributes']['disabled']) ) {
                             echo 'disabled="disabled"';
                         }

                        ?>>
                        <?php echo $option_label; ?>
                    </label>
                </li>
            <?php } ?>
        </ul>
    </div>
    <?php
}

This disabled the group of checkboxes.

Upvotes: 0

Sushil Adhikari
Sushil Adhikari

Reputation: 794

woocommerce_form_field() function doesn't provide such ability at checkbox input type, see here:

case 'checkbox':
                $field = '<label class="checkbox ' . implode( ' ', $args['label_class'] ) . '" ' . implode( ' ', $custom_attributes ) . '>
                        <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>';
  • You can take help of javascript to disabled the input field, either use wp_add_inline_script() or can add Javascript code normally on some .js files.

Disbled input field using jQuery

  • or Can rewrite woocommerce_form_field() function at your plugin files. may not work adding on child theme functional file, not sure on this.

Thanks

Upvotes: 1

Full Stop
Full Stop

Reputation: 853

Try this jQuery

jQuery(function($){
   $('#Chekbox_Id_Here').attr('checked', false);
});

I hope it's helps you

Thanks.

Upvotes: 0

Related Questions