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' ));
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'll try to be short:
Multilingual Wordpress site with custom post type registered in functions.php and CMB2 for creating custom forms. The plugin for multilingual is q-translate-x and also using CMB2-qTranslate.
My problem is that I'm losing all line breaks and all <p> tags when I switch the language but it only fails if the editor is contained in a repeatable group. If I add the wysiwyg edit as normal field It works fine.
-Relevant code for the normal field (this works fine):
$cmb_tb->add_field( array(
'name' => esc_html__( 'Historia', 'cmb2' ),
'desc' => esc_html__( 'field description (optional)', 'cmb2' ),
'id' => $prefix . 'historia',
'type' => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, 'editor_class' => 'cmb2-qtranslate'),
) );
-Relevant code for repeatable field (this loses line breaks and <p> tags on language switching):
<pre><code>
$group_field_id = $cmb_tb->add_field( array(
'id' => 'Fincas',
'type' => 'group',
'description' => __( 'Fincas', 'cmb2' ),
// 'repeatable' => false, // use false if you want non-repeatable group
'options' => array(
'group_title' => __( 'Finca {#}', 'cmb2' ), // since version 1.1.4, {#} gets replaced by row number
'add_button' => __( 'AƱadir otra Finca', 'cmb2' ),
'remove_button' => __( 'Eliminar Finca', 'cmb2' ),
// 'sortable' => true, // beta
'closed' => false, // true to have the groups closed by default
),
) );
$cmb_tb->add_group_field($group_field_id, array(
'name' => esc_html__( 'Nombre Finca', 'cmb2' ),
'desc' => esc_html__( '', 'cmb2' ),
'id' => $prefix . 'nombre_finca',
'type' => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, 'editor_class' => 'cmb2-qtranslate')
) );
</code></pre>
This is driving me crazy. I've created a sample website with this problem for testing. I can give access to anyone who feels that can help.
Many thanks in advance.
Finally, I figured out.
It was as easy to add 'wpautop' => false to options array
I'm looking to add three custom date fields using select. So the first one would be the day, second would be month and the third would be the year. However, I'm not sure how to add a code without manually entering all years pluss to have three fields count as one.
I have a code I used to add a custom field 'gender' any way to something similar for a dob select fields described above.
add_action('woocommerce_before_checkout_billing_form', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout ) {
woocommerce_form_field( 'apgen', array(
'type' => 'select',
'class' => array( 'ap-drop' ),
'label' => __( 'Gender' ),
'options' => array(
'blank' => __( 'Select Gender', 'ap' ),
'male' => __( 'Male', 'ap' ),
'Female' => __( 'Female', 'ap' ),
'non-binary' => __( 'Non-binary', 'ap' )
)
),
$checkout->get_value( 'apgen' ));
}
Woocommerce has now a "date" field type with a very specific behavior, that has inside its field, 3 selectable number field type (days, month and year) that you can select individually, with a showable date picker (see screenshots below):
add_filter('woocommerce_checkout_fields', 'wps_add_checkout_field');
function wps_add_checkout_field( $fields ) {
// Select field (Gender)
$fields['billing']['billing_gender'] = array(
'type' => 'select',
'class' => array( 'form-row-wide' ),
'label' => __( 'Gender' ),
'required' => true,
'priority' => 3,
'options' => array(
'' => __( 'Select Gender', 'ap' ),
'male' => __( 'Male', 'ap' ),
'Female' => __( 'Female', 'ap' ),
'non-binary' => __( 'Non-binary', 'ap' )
),
);
// Date field (with 3 number fields with a datepicker)
$fields['billing']['billing_date'] = array(
'type' => 'date',
'class' => array( 'form-row-wide' ),
'label' => __( 'Date' ),
'required' => true,
'priority' => 3,
);
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
You will get something like:
Rolling in the date field:
Selecting days and using to increase or decrease days:
Selecting months and increasing value (or typing a value):
Selecting years and increasing value (or typing a value):
Making appear the date picker and select a date:
Other similar hooks that can be used:
add_filter('woocommerce_billing_fields', 'wps_add_date_type_checkout_field');
function wps_add_date_type_checkout_field( $fields ) {
// Date field (with 3 number fields with a datepicker)
$fields['billing_date'] = array(
'type' => 'date',
'class' => array( 'form-row-wide' ),
'label' => __( 'Date' ),
'priority' => 5,
);
return $fields;
}
Or
add_action('woocommerce_before_checkout_billing_form', 'wps_add_date_type_checkout_field');
function wps_add_date_type_checkout_field( $checkout ) {
woocommerce_form_field( 'billing_date', array(
'type' => 'date',
'class' => array( 'form-row-wide' ),
'label' => __( 'Gender' ),
), $checkout->get_value( 'billing_date' ));
}
Or before (or after) order notes:
add_action('woocommerce_before_order_notes', 'wps_add_date_type_checkout_field');
// add_action('woocommerce_after_order_notes', 'wps_add_date_type_checkout_field');
function wps_add_date_type_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field">';
woocommerce_form_field( 'billing_date', array(
'type' => 'date',
'class' => array('my-field-class form-row-wide'),
'label' => __('Date'),
), $checkout->get_value( 'billing_date' ));
echo '</div>';
}
Related docs: Customizing Woocommerce checkout fields using actions and filters
I adda ed the below code to function.php page and it shows the select box at checkout page fine. But how can i get the value of selected item at the thank you page.
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['billing']['point_side'] = array(
'label' => __(
'<strong>Select Where Your Points Should Go</strong>',
'woocommerce'
),
'placeholder' => _x(
'custom_field', 'placeholder',
'woocommerce'
),
'required' => true,
'clear' => false,
'type' => 'select',
'class' => array('form-row-wide'),
'options' => array(
'default1' => __('Defult1', 'woocommerce'),
'ls' => __('Left Side', 'woocommerce'),
'rs' => __('Right Side', 'woocommerce')
)
);
return $fields;
}
Any helps appreciated.
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