i have the following function adding a checkbox to the woocommerce checkout form:
woocommerce_form_field( 'email_signup', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Newsletter Signup?'),
), $checkout->get_value( 'email_signup' ));
I would like to make the checkbox selected by default. Is there a way of doing it through woocommerce form_field options? Or will i need to use javascript?
Adding 'default' => 1 should do the trick
woocommerce_form_field( 'email_signup', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Newsletter Signup?'),
'checked' => 'checked',
'default' => 1,
), $checkout->get_value( 'email_signup' ));
Have you tried:
woocommerce_form_field( 'email_signup', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Newsletter Signup?'),
'checked' => 'checked',
), $checkout->get_value( 'email_signup' ));
By including:
'checked' => 'checked',
'default' => 1,
in array, would do the trick.. Voila!
you can certainly do that, with this plugin
Also you can actually insert your very own two options for checkbox. "Yes" or "No" etc
Related
This is my first WP plugin. And I have been able to get it working perfectly with the settings page and also showing the shipping method on the checkout page. On a requested feature I need to have another field in my settings page that will have multiple checkboxes to allow users to service types they prefer on their site. This is an example of what I am trying to achieve:
The shot above was taken on from the Woocommerce product settings page. Hence I had to look at the code from WC's files. I found the snippet below but I can't get it to work on my settings page:
array(
'title' => __( 'Enable reviews', 'woocommerce' ),
'desc' => __( 'Enable product reviews', 'woocommerce' ),
'id' => 'woocommerce_enable_reviews',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => 'start',
'show_if_checked' => 'option',
),
array(
'desc' => __( 'Show "verified owner" label on customer reviews', 'woocommerce' ),
'id' => 'woocommerce_review_rating_verification_label',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => '',
'show_if_checked' => 'yes',
'autoload' => false,
),
array(
'desc' => __( 'Reviews can only be left by "verified owners"', 'woocommerce' ),
'id' => 'woocommerce_review_rating_verification_required',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'end',
'show_if_checked' => 'yes',
'autoload' => false,
),
All the fields I have currently displayed on my settings page was done in this manner:
$this->form_fields = array(
'is_enabled' => array(
'title' => __('Enable', 'plugin_id'),
'type' => 'checkbox',
'description' => __('Enable this shipping.','plugin_id'),
'default' => 'no'
),
'api_key' => array(
'title' => __('API Key', 'plugin_id'),
'type' => 'text',
'description' => __('Obtain API key from Lastmal DIP','plugin_id'),
'default' => null
),
...
)
How can I add the multiple checkboxes to my fields and save them along with the other settings I have?
I am making an input form in WooCommerce using a function called woocommerce_form_field() for an input type number field like below:
woocommerce_form_field(
'left_far',
array(
'type' => 'number',
'class' => array('left_far', 'left_far woocommerce-form-row woocommerce-form-row--last form-row form-row-last'),
'required' => false,
'label' => 'OOOOO',
),
get_user_meta(get_current_user_id(), 'left_far', true)
);
Now I would like to be able to allow decimal float numbers to be inputed in this field. Is it possible? What should I do?
You need to use 'custom_attributes' argument with attribute 'step' set to 'any' as follows:
woocommerce_form_field( 'left_far', array(
'type' => 'number',
'label' => 'OOOOO',
'class' => array('left_far', 'left_far woocommerce-form-row woocommerce-form-row--last form-row form-row-last'),
'custom_attributes' => array( 'step' => 'any', 'min' => '0' ),
'required' => false,
), get_user_meta( get_current_user_id(), 'left_far', true ) );
Tested and works.
Note: 'min' argument define the starting allowed value
I'm trying to add fields to an options page through PHP, and I can't get it to work, I've tried nearly everything by now, but it just won't work.
I hope you can help, my php looks like this:
if (function_exists('acf_add_options_page')) {
$option_page = acf_add_options_page(array(
'page_title' => 'Indstillinger',
'menu_title' => 'Indstillinger',
'menu_slug' => 'options',
'capability' => 'edit_posts',
'redirect' => false
));
}
function my_acf_add_local_field_groups() {
acf_add_local_field_group(array(
'key' => 'group_1',
'title' => 'My Group',
'fields' => array(
array(
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => 'text',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'options',
),
),
),
));
}
add_action('acf/init', 'my_acf_add_local_field_groups');
What am I doing wrong here?
I have found out.
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'theme-general-settings',
),
),
),
Here in the value field just add the "menu slug" that you have registered for the options page. Like 'theme-header-settings', 'theme-general-settings' are you requirement. I hope you understand.
I think you want
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'options',
),
),
),
The way you have added Option page, it seems that you have installed ACF plugin.
You can add any type of field on your option page using ACF plugin as well.
Check my attached screen shot, You need to set following condition for the field groups:
I'm trying to add a bunch of custom fields at the end of the checkout form that collects additional data we'd need to process an order.
This is what I have:
add_action( 'woocommerce_after_order_notes', 'student_info_fields' );
function student_info_fields( $checkout ) {
echo '<div id="student_info"><span>The following information below will be used to create an account.</span>';
//This adds a student_name field
woocommerce_form_field( 'student_name', array(
'type' => 'text',
'class' => array('form-row-wide'),
'label' => __('Student Name'),
'placeholder' => __('eg: "John Smith", "Johnny", etc'),
'required' => 'true',
), $checkout->get_value( 'student_name' ));
/* I have two other text fields here that follow the same syntax as student_name */
//This adds a student_gender field
woocommerce_form_field( 'student_gender', array(
'type' => 'select',
'label' => __('Gender', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => 'true',
'options' => array(
'male' => __('Male', 'woocommerce' ),
'female' => __('Female', 'woocommerce' )
),
), $checkout->get_value( 'student_gender' ));
echo '</div>';
}
The text fields all seem to work, but when I add the student_gender field, my page breaks (all white screen). I'm a little skeeved out by calling the options array within the student_gender array, as well as calling the $checkout ->get_value... line after I declare each field, but I simply don't know what to do.
Any direction you can give me would be so helpful to cracking this nut. Thanks for sticking with it!
I looked at it and figured out what I was screwing up. That is to say, it pointed me toward my label declaration, which I don't think was using the proper syntax.
I changed my code to:
//This adds a student_gender field
woocommerce_form_field( 'student_gender', array(
'type' => 'select',
'label' => __('Student Gender'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => 'true',
'options' => array(
'male' => __('Male', 'woocommerce' ),
'female' => __('Female', 'woocommerce' )
),
), $checkout->get_value( 'student_grade' ));
I'm new to ZF2, I am rewriting a simple application previously writen with ZF1. I have a form wich I want to customize, because the default input/textearea width and heigth are not enought. Here my code for some fields of my form:
$this->add(array(
'name' => 'page',
'type' => 'Number',
'options' => array(
'label' => 'Page',
),
));
$this->add(array(
'name' => 'name',
'type' => 'Text',
'options' => array(
'label' => 'Name',
),
));
$this->add(array(
'name' => 'notes',
'type' => 'Textarea',
'options' => array(
'label' => 'Notes',
),
));
How can I specify a width for my inputs elements, and the attributes cols and rows for my textarea ?
$this->add(array(
'name' => 'name',
'type' => 'Text',
'options' => array(
'label' => 'Name',
),
'attributes' => array
(
'maxlength' => 250,
'type' => 'text',
'class' => 'class-name'
),
));
You can add an attributes array, and specify a class which can be used to edit things like width for your input elements. I believe you can also add 'rows' and 'cols' to this as well.