Custom checkout random select field in Woocommerce - php

I have a simple WordPress site that is using WooCommerce.
I would like to add the functionality onto the /checkout page a custom woocommerce field. This field would ideally be of type Select with multiple options. This is not a problem as I am able to add the below code into my child theme's functions.php to create this.
function customise_checkout_field($checkout)
{
// Heading for form
echo '<p>Custom Question Heading</p>';
woocommerce_form_field( 'questionOne', array(
'type' => 'select',
'class' => array( 'custom-dev-select'),
'label' => 'This is the question',
'options' => array(
'blank' => 'Choose One',
'value1' => 'Answer 1,
'value2' => 'Answer 2
),
'required' => true
)
);
$checkout->get_value( $random_question );
}
This will produce a single select option with the above attributes.
The issue is, I would like to have say X3 of these 'woocommerce_form_field's, each with different a different label/question and different options. For example;
Question 1: Is an apple a:
Option 1: Fruit
Option 2: Meat
Option 3: Veg
Question 2: Some question
Option 1: lorem
Option 2: lorem
Option 3: lorem
And then each time the page is loaded or refreshed etc a different question is loaded.
I have tried adding multiple 'woocommerce_form-field's into an array and using array_rand etc however this does not work. Here is some example code I have in place which currently is not working, but you get the idea of how I would like it to work.
function customise_checkout_field($checkout)
{
// Heading for form
echo '<p>Custom Question Heading</p>';
$questions = array(
"question1" => array(
"This is question one",
"Choice 1",
"Choice 2"
),
"question2" => array(
"This is question Two",
"Choice 1.1",
"Choice 2.1"
),
"question3" => array(
"label" => "This is question Three",
"Choice 1.2",
"Choice 2.2"
),
);
$random_question = $questions[array_rand($questions)];
$selected_label = $random_question[0];
$selected_answer = $random_question[1];
$selected_answer2 = $random_question[2];
woocommerce_form_field( 'questionOne', array(
'type' => 'select',
'class' => array( 'custom-dev-select'),
'label' => $selected_label,
'options' => array(
'blank' => 'Choose One',
'value1' => $selected_answer,
'value2' => $selected_answer2
),
'required' => true
)
);
$checkout->get_value( $random_question );
}
add_action('woocommerce_after_order_notes', 'customise_checkout_field');
Any help would be greatly appreciated. WordPress, woocommerce & php is fairly new to me as this is not my main language to use.

To make a random checkout select field (as a random question) with validation and saving the data as custom order meta data, use the following:
add_action( 'woocommerce_after_order_notes', 'custom_select_field_with_random_options', 10, 1 );
function custom_select_field_with_random_options( $checkout )
{
// Heading for form
echo '<h4>' . __("Custom Question Heading", "woocommerce") . '</h4>';
$questions = array(
'1' => array(
'label' => __("one", "woocommerce"),
'options' => array(
'value1' => __("Choice 1.1", "woocommerce"),
'value2' => __("Choice 1.2", "woocommerce"),
),
),
'2' => array(
'label' => __("two", "woocommerce"),
'options' => array(
'value1' => __("Choice 2.1", "woocommerce"),
'value2' => __("Choice 2.2", "woocommerce"),
),
),
'3' => array(
'label' => __("three", "woocommerce"),
'options' => array(
'value1' => __("Choice 3.1", "woocommerce"),
'value2' => __("Choice 3.2", "woocommerce"),
),
),
);
$key = array_rand($questions); // Random key
$question = $questions[$key]; // The question data array
$label = $question['label'];
$default = array( '' => __("Choose an answer", "woocommerce") );
$options = $default + $question['options'];
woocommerce_form_field( 'question_'.$key, array(
'type' => 'select',
'class' => array( 'custom-dev-select'),
'label' => __("This is the question", "woocommerce") . ' ' . $label,
'options' => $options,
'required' => true
), $checkout->get_value( 'question_'.$key ) );
echo '<input type="hidden" name="question_key" value="'.$key.'">';
}
// Custom Checkout fields validation
add_action('woocommerce_checkout_process', 'custom_checkout_select_field_validation');
function custom_checkout_select_field_validation() {
if ( isset($_POST['question_key']) ) {
$key = esc_attr( $_POST['question_key'] );
if ( isset($_POST['question_'.$key]) && empty($_POST['question_'.$key]) )
wc_add_notice( '<strong>'. __("Please select a value", "woocommerce") . '</strong>', 'error' );
}
}
// Save custom checkout fields the data to the order
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_meta', 10, 2 );
function custom_checkout_field_update_meta( $order, $data ){
if ( isset($_POST['question_key']) ) {
$key = esc_attr( $_POST['question_key'] );
if ( isset($_POST['question_'.$key]) && ! empty($_POST['question_'.$key]) ) {
$order->update_meta_data( '_question_value', esc_attr( $_POST['question_'.$key] ) );
$order->update_meta_data( '_question_key', $key );
}
}
}
// display the random question data in the order admin panel
add_action( 'woocommerce_admin_order_data_after_order_details', 'display_question_to_admin_order', 10, 1 );
function display_question_to_admin_order( $order ){
if( $key = $order->get_meta( '_question_key' ) ) {
if( $value = $order->get_meta( '_question_value' ) ) {
echo '<br style="clear:both">
<p><strong>' . __( "Random question", "woocommerce" ) . ' '. $key . ':</strong> ' . $value . '</p>';
}
}
}
Code goes in function.php file of your active child theme (active theme). Tested and works.

Related

Add a custom WooCommerce settings page, including page sections

I'm trying to add a custom settings tab to the WooCommerce settings screen. Basically I want to achieve a similar thing to the Products settings tab, with the subsections/subtabs:
I haven't been able to find any decent documentation on how to do this but I've been able to add a custom tab using this snippet:
class WC_Settings_Tab_Demo {
public static function init() {
add_filter( 'woocommerce_settings_tabs_array', __CLASS__ . '::add_settings_tab', 50 );
}
public static function add_settings_tab( $settings_tabs ) {
$settings_tabs['test'] = __( 'Settings Demo Tab', 'woocommerce-settings-tab-demo' );
return $settings_tabs;
}
}
WC_Settings_Tab_Demo::init();
Based on what I've dug up from various threads/tutorials, I've been trying to add the sections/subtabs to the new settings tab something like this:
// creating a new sub tab in API settings
add_filter( 'woocommerce_get_sections_test','add_subtab' );
function add_subtab( $sections ) {
$sections['custom_settings'] = __( 'Custom Settings', 'woocommerce-custom-settings-tab' );
$sections['more_settings'] = __( 'More Settings', 'woocommerce-custom-settings-tab' );
return $sections;
}
// adding settings (HTML Form)
add_filter( 'woocommerce_get_settings_test', 'add_subtab_settings', 10, 2 );
function add_subtab_settings( $settings, $current_section ) {
// $current_section = (isset($_GET['section']) && !empty($_GET['section']))? $_GET['section']:'';
if ( $current_section == 'custom_settings' ) {
$custom_settings = array();
$custom_settings[] = array( 'name' => __( 'Custom Settings', 'text-domain' ),
'type' => 'title',
'desc' => __( 'The following options are used to ...', 'text-domain' ),
'id' => 'custom_settings'
);
$custom_settings[] = array(
'name' => __( 'Field 1', 'text-domain' ),
'id' => 'field_one',
'type' => 'text',
'default' => get_option('field_one'),
);
$custom_settings[] = array( 'type' => 'sectionend', 'id' => 'test-options' );
return $custom_settings;
} else {
// If not, return the standard settings
return $settings;
}
}
I've been able to add new subsections to the Products tab using similar code to the above, but it isn't working for my new custom tab. Where am I going wrong here?
1) To add a setting tab with sections, you can firstly use the woocommerce_settings_tabs_array filter hook:
// Add the tab to the tabs array
function filter_woocommerce_settings_tabs_array( $settings_tabs ) {
$settings_tabs['my-custom-tab'] = __( 'My custom tab', 'woocommerce' );
return $settings_tabs;
}
add_filter( 'woocommerce_settings_tabs_array', 'filter_woocommerce_settings_tabs_array', 99 );
2) To add new sections to the page, you can use the woocommerce_sections_{$current_tab} composite hook where {$current_tab} need to be replaced by the key slug that is set in the first function:
// Add new sections to the page
function action_woocommerce_sections_my_custom_tab() {
global $current_section;
$tab_id = 'my-custom-tab';
// Must contain more than one section to display the links
// Make first element's key empty ('')
$sections = array(
'' => __( 'Overview', 'woocommerce' ),
'my-section-1' => __( 'My section 1', 'woocommerce' ),
'my-section-2' => __( 'My section 2', 'woocommerce' )
);
echo '<ul class="subsubsub">';
$array_keys = array_keys( $sections );
foreach ( $sections as $id => $label ) {
echo '<li>' . $label . ' ' . ( end( $array_keys ) == $id ? '' : '|' ) . ' </li>';
}
echo '</ul><br class="clear" />';
}
add_action( 'woocommerce_sections_my-custom-tab', 'action_woocommerce_sections_my_custom_tab', 10 );
3) For adding the settings, as well as for processing/saving, we will use a custom function, which we will then call:
// Settings function
function get_custom_settings() {
global $current_section;
$settings = array();
if ( $current_section == 'my-section-1' ) {
// My section 1
$settings = array(
// Title
array(
'title' => __( 'Your title 1', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_1'
),
// Text
array(
'title' => __( 'Your title 1.1', 'text-domain' ),
'type' => 'text',
'desc' => __( 'Your description 1.1', 'woocommerce' ),
'desc_tip' => true,
'id' => 'custom_settings_1_text',
'css' => 'min-width:300px;'
),
// Select
array(
'title' => __( 'Your title 1.2', 'woocommerce' ),
'desc' => __( 'Your description 1.2', 'woocommerce' ),
'id' => 'custom_settings_1_select',
'class' => 'wc-enhanced-select',
'css' => 'min-width:300px;',
'default' => 'aa',
'type' => 'select',
'options' => array(
'aa' => __( 'aa', 'woocommerce' ),
'bb' => __( 'bb', 'woocommerce' ),
'cc' => __( 'cc', 'woocommerce' ),
'dd' => __( 'dd', 'woocommerce' ),
),
'desc_tip' => true,
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_1'
),
);
} elseif ( $current_section == 'my-section-2' ) {
// My section 2
$settings = array(
// Title
array(
'title' => __( 'Your title 2', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_2'
),
// Text
array(
'title' => __( 'Your title 2.2', 'text-domain' ),
'type' => 'text',
'desc' => __( 'Your description 2.1', 'woocommerce' ),
'desc_tip' => true,
'id' => 'custom_settings_2_text',
'css' => 'min-width:300px;'
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_2'
),
);
} else {
// Overview
$settings = array(
// Title
array(
'title' => __( 'Overview', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_overview'
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_overview'
),
);
}
return $settings;
}
3.1) Add settings, via the woocommerce_settings_{$current_tab} composite hook:
// Add settings
function action_woocommerce_settings_my_custom_tab() {
// Call settings function
$settings = get_custom_settings();
WC_Admin_Settings::output_fields( $settings );
}
add_action( 'woocommerce_settings_my-custom-tab', 'action_woocommerce_settings_my_custom_tab', 10 );
3.2) Process/save the settings, via the woocommerce_settings_save_{$current_tab} composite hook:
// Process/save the settings
function action_woocommerce_settings_save_my_custom_tab() {
global $current_section;
$tab_id = 'my-custom-tab';
// Call settings function
$settings = get_custom_settings();
WC_Admin_Settings::save_fields( $settings );
if ( $current_section ) {
do_action( 'woocommerce_update_options_' . $tab_id . '_' . $current_section );
}
}
add_action( 'woocommerce_settings_save_my-custom-tab', 'action_woocommerce_settings_save_my_custom_tab', 10 );
Result:
Based on:
Implement a custom WooCommerce settings page, including page sections
woocommerce/includes/admin/settings/

Custom checkout billing field on email notifications

In WooCommerce, based on "Dynamic synched custom checkout select fields in WooCommerce" answer code, I am adding some custom fields on checkout which will be displayed under the billing section on the e-mail confirmation
Here is my actual code:
add_action( 'woocommerce_after_checkout_billing_form', 'add_checkout_custom_fields', 20, 1 );
function add_checkout_custom_fields( $checkout) {
$domain = 'woocommerce'; // The domain slug
// First Select field (Master)
woocommerce_form_field( 'delivery_one', array(
'type' => 'select',
'label' => __( 'Art der Lieferung' , $domain),
'class' => array( 'form-row-first' ),
'required' => true,
'options' => array(
'' => __( 'Wählen Art der Lieferung.', $domain ),
'A' => __( 'Hauszustellung', $domain ),
'B' => __( 'Selbst Abholung', $domain ),
),
), $checkout->get_value( 'delivery_one' ) );
// Default option value
$default_option2 = __( 'Wählen Sie Zeitbereich.', $domain );
// Dynamic select field options for Javascript/jQuery
$options_0 = array( '' => $default_option2 );
$options_a = array(
'' => $default_option2,
'1' => __( '09:00-11:00', $domain ),
'2' => __( '10:00-12:00', $domain ),
'3' => __( '11:00-13:00', $domain ),
'4' => __( '12:00-14:00', $domain ),
'5' => __( '13:00-15:00', $domain ),
'6' => __( '14:00-16:00', $domain ),
'7' => __( '15:00-17:00', $domain ),
);
$options_b = array(
'' => $default_option2,
'1' => __( '01:00', $domain ),
'2' => __( '02:00', $domain ),
'3' => __( '03:00', $domain ),
'4' => __( '04:00', $domain ),
'5' => __( '05:00', $domain ),
'6' => __( '06:00', $domain ),
'7' => __( '07:00', $domain ),
'8' => __( '08:00', $domain ),
'9' => __( '09:00', $domain ),
'10' => __( '10:00', $domain ),
'11' => __( '11:00', $domain ),
'12' => __( '12:00', $domain ),
'13' => __( '13:00', $domain ),
'14' => __( '14:00', $domain ),
'15' => __( '15:00', $domain ),
'16' => __( '16:00', $domain ),
'17' => __( '17:00', $domain ),
'18' => __( '18:00', $domain ),
'19' => __( '19:00', $domain ),
'20' => __( '20:00', $domain ),
'21' => __( '21:00', $domain ),
'22' => __( '22:00', $domain ),
'23' => __( '23:00', $domain ),
'24' => __( '24:00', $domain ),
);
// Second Select field (Dynamic Slave)
woocommerce_form_field( 'delivery_two', array(
'type' => 'select',
'label' => __( 'Zeitspanne', $domain ),
'class' => array( 'form-row-last' ),
'required' => true,
'options' => $options_0,
), $checkout->get_value( 'delivery_two' ) );
$required = esc_attr__( 'required', 'woocommerce' );
// jQuery code
?>
<script>
jQuery(function($){
var op0 = <?php echo json_encode($options_0); ?>,
opa = <?php echo json_encode($options_a); ?>,
opb = <?php echo json_encode($options_b); ?>,
select1 = 'select[name="delivery_one"]',
select2 = 'select[name="delivery_two"]';
// Utility function to fill dynamically the select field options
function dynamicSelectOptions( opt ){
var options = '';
$.each( opt, function( key, value ){
options += '<option value="'+key+'">'+value+'</option>';
});
$(select2).html(options);
}
// 1. When dom is loaded we add the select field option for "A" value
// => Disabled (optional) — Uncomment below to enable
// dynamicSelectOptions( opa );
// 2. On live selection event on the first dropdown
$(select1).change(function(){
if( $(this).val() == 'A' )
dynamicSelectOptions( opa );
else if( $(this).val() == 'B' )
dynamicSelectOptions( opb );
else
dynamicSelectOptions( op0 ); // Reset to default
});
});
</script>
<?php
}
// Check checkout custom fields
add_action( 'woocommerce_checkout_process', 'wps_check_checkout_custom_fields', 20 ) ;
function wps_check_checkout_custom_fields() {
// if custom fields are empty stop checkout process displaying an error notice.
if ( empty($_POST['delivery_one']) || empty($_POST['delivery_two']) ){
$notice = __( 'Bitte wählen Sie die Versandart oder den Stundenbereich' );
wc_add_notice( '<strong>' . $notice . '</strong>', 'error' );
}
}
My custom fields and their values, are shown on the checkout form and on the order pages on back end. So far everything works great.
But the problem is that the e-mail that I receive does not contain the custom fields and their values.
How can I display that custom checkout billing fields on email notifications?
Is this code correct?
add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 20, 2 );
function add_custom_email_format_string( $string, $email ) {
// The post meta key used to save the value in the order post meta data
$meta_key = '_delivery_one';
// Get the instance of the WC_Order object
$order = $email->object;
// Get the value
$value = $order->get_meta($meta_key) ? $order->get_meta($meta_key) : '';
// Additional subject placeholder
$new_placeholders = array( '{delivery_one}' => $value );
return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
}
You have to define a new placeholder that can be parsed.
add_filter( 'woocommerce_email_format_string' , 'add_custom_email_format_string', 20, 2 );
function add_custom_email_format_string( $string, $email ) {
// The post meta key used to save the value in the order post meta data
$meta_key = '_billing_field_newfield';
// Get the instance of the WC_Order object
$order = $email->object;
// Get the value
$value = $order->get_meta($meta_key) ? $order->get_meta($meta_key) : '';
// Additional subject placeholder
$new_placeholders = array( '{billing_field_newfield}' => $value );
// Return the clean replacement value string for "{billing_field_newfield}" placeholder
return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
}
Code goes in function.php file of your active child theme (or active theme). It will works.
Then in Woocommerce > Settings > Emails > "New Order" notification, you will be able to use the dynamic placeholder {billing_field_newfield}…
You should first save input value at the database as an order meta see here and then you can get the meta value in email template. All the email template of woocommerce is customizable and located at plugins/woocommerce/emails/
You can use this hook to store custom input field at order meta
do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );
Thanks

WooCommerce: Display custom checkout fields if specific product IDs are in cart

I am running an online-store selling grills. Only the grills are shipped via a specific carrier where additional informations are needed.
I managed to display a dropdown when a certain product_ID is in the cart. When a specific value is selected a text-area is displayed.
This should happen for about 10 products, not just one.
After reading a number of threads and searching the web, i cant figure out, how to add more than one product_ID.
add_action( 'woocommerce_after_order_notes', 'grills_versandauswahl_checkout_field' );
function grills_versandauswahl_checkout_field( $checkout ) {
$grill_in_cart = grills_is_conditional_product_in_cart ( 125 );
if ( $grill_in_cart === true ) {
echo '<div id="my_custom_checkout_field"><h3>' . __( 'Versandoptionen' ) . '</h3><p style="margin: 0 0 8px;">text</p>';
woocommerce_form_field( 'versandoption', array(
'type' => 'select',
'class' => array( 'wps-drop' ),
'label' => __( 'Versandoptionen' ),
'required' => true,
'options' => array(
'blank' => __( 'Auswählen', 'wps' ),
'fixtermin' => __( 'Fixtermin', 'wps' ),
'avis' => __( 'Telefonisches Avis', 'wps' ),
)
), $checkout->get_value( 'versandoption' ) );
woocommerce_form_field( 'inscription_textbox', array(
'type' => 'textarea',
'class' => array( 'inscription-text form-row-wide' ),
'label' => __( 'Wunschtermin / Ablageort' ),
), $checkout->get_value( 'inscription_textbox' ) );
echo '</div>';
}
}
function grills_is_conditional_product_in_cart( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;
//flag no book in cart
$grill_in_cart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id === $product_id ) {
//book is in cart!
$grill_in_cart = true;
}
}
return $grill_in_cart;
}
To make your conditional function work for many product Ids use the following:
// Custom conditional function
function grills_in_cart( $product_ids ) {
foreach ( WC()->cart->get_cart() as $item ) {
if ( array_intersect($product_ids, array( $item['product_id'], $item['variation_id']) ) ) {
return true;
}
}
return false;
}
// Conditionally display custom checkout fields for specific product IDS
add_action( 'woocommerce_after_order_notes', 'grills_versandauswahl_checkout_field' );
function grills_versandauswahl_checkout_field( $checkout ) {
// Here define the targeted product IDs in this array
$targeted_ids = array( 125, 132, 154 );
$text_domain = 'woocommerce';
if ( grills_in_cart( $targeted_ids ) ) {
echo '<div id="my_custom_checkout_field">
<h3>' . __( "Versandoptionen", $text_domain ) . '</h3>
<p style="margin: 0 0 8px;">' . __( "text", $text_domain ) . '</p>';
woocommerce_form_field( 'versandoption', array(
'type' => 'select',
'class' => array('wps-drop'),
'label' => __( "Versandoptionen", $text_domain ),
'required' => true,
'options' => array(
'blank' => __( "Auswählen", $text_domain ),
'fixtermin' => __( "Fixtermin", $text_domain ),
'avis' => __( "Telefonisches Avis", $text_domain ),
),
), $checkout->get_value( 'versandoption' ) );
woocommerce_form_field( 'inscription_textbox', array(
'type' => 'textarea',
'class' => array( 'inscription-text form-row-wide' ),
'label' => __( 'Wunschtermin / Ablageort', $text_domain ),
), $checkout->get_value( 'inscription_textbox' ) );
echo '</div>';
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Note: $global $woocommerce; + $woocommerce->cart has been replaced by WC()->cart

Dynamic synched custom checkout select fields in WooCommerce

In Woocommerce I have been able to add 2 custom dropdowns list in checkout page:
add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout) {
echo '<h2>'.__('Next Day Delivery').'</h2>';
woocommerce_form_field( 'City', array(
'type' => 'select',
'class' => array( 'wps-drop' ),
'label' => __( 'Delivery options' ),
'options' => array(
'blank' => __( 'Select a day part', 'wps' ),
'A' => __( 'A', 'wps' ),
'B' => __( 'B', 'wps' ),
'C' => __( 'C', 'wps' )
)
),
$checkout->get_value( 'City' ));
}
add_action('woocommerce_before_order_notes', 'wps_add_select_checkout1_field');
function wps_add_select_checkout1_field( $checkout1) {
//echo '<h2>'.__('Next Day Delivery').'</h2>';
woocommerce_form_field( 'Dis', array(
'type' => 'select',
'class' => array( 'wps-drop' ),
'label' => __( 'Delivery options' ),
'options' => array(
'blank' => __( 'Select a day part', 'wps' ),
'A' => __( 'ro', 'wps' ),
'B' => __( 'wa', 'wps' ),
'C' => __( 'da', 'wps' )
)
),
$checkout1->get_value( 'Dis' ));
}
//* Process the checkout
add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
function wps_select_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error.
if ($_POST['City'] == "blank")
wc_add_notice( '<strong>Please select a day part under Delivery options</strong>', 'error' );
}
I would like to generate dynamically the options of the second dropdown list, based on the selected <option> from the first dropdown list.
Example of what I need:
If <option> "A" is selected in the 1st dropdown list, The 2nd dropdown list will show dynamically a specific set of options.
If <option> "C" is selected in the 1st dropdown list, The 2nd dropdown list will show dynamically a different set of options.
And so on …
Is that possible? Where do I have to start?
Any help is appreciated.
I have merged your 2 first functions as they use the same hook.
In this merged function I have added:
The "required" option to both fields.
Changed the slugs for both fields as for example "City" throw a woocommerce error.
different sets of "options" arrays that I pass to javascript (one for each available <option> in the first select field).
Some jQuery code that create dynamically the set of options in the 2nd select field, based on the selected <option> of the first select field.
I have change a bit the condition for the If statement in the last function.
Here is the revisited code:
// Add checkout custom select fields
add_action( 'woocommerce_before_order_notes', 'add_checkout_custom_fields', 20, 1 );
function add_checkout_custom_fields( $checkout) {
$domain = 'woocommerce'; // The domain slug
echo '<h2>'.__( 'Next Day Delivery', $domain ).'</h2>';
// First Select field (Master)
woocommerce_form_field( 'delivery_one', array(
'type' => 'select',
'label' => __( 'Delivery options one' , $domain),
'class' => array( 'form-row-wide' ),
'required' => true,
'options' => array(
'' => __( 'Please select a value', $domain ),
'A' => __( 'A', $domain ),
'B' => __( 'B', $domain ),
'C' => __( 'C', $domain ),
),
),
$checkout->get_value( 'delivery_one' ) );
// Default option value
$default_option2 = __( 'Please select a value', $domain );
// Dynamic select field options for Javascript/jQuery
$options_0 = array( '' => $default_option2 );
$options_a = array(
'' => $default_option2,
'D' => __( 'D', $domain ),
'E' => __( 'E', $domain ),
'F' => __( 'F', $domain ),
'G' => __( 'G', $domain ),
);
$options_b = array(
'' => $default_option2,
'H' => __( 'H', $domain ),
'I' => __( 'I', $domain ),
'J' => __( 'J', $domain ),
);
$options_c = array(
'' => $default_option2,
'K' => __( 'K', $domain ),
'L' => __( 'L', $domain ),
'M' => __( 'M', $domain ),
);
// Second Select field (Dynamic Slave)
woocommerce_form_field( 'delivery_two', array(
'type' => 'select',
'label' => __( 'Delivery options two', $domain ),
'class' => array( 'form-row-wide' ),
'required' => true,
'options' => $options_0,
),
$checkout->get_value( 'delivery_two' ) );
$required = esc_attr__( 'required', 'woocommerce' );
// jQuery code
?>
<script>
jQuery(function($){
var op0 = <?php echo json_encode($options_0); ?>,
opa = <?php echo json_encode($options_a); ?>,
opb = <?php echo json_encode($options_b); ?>,
opc = <?php echo json_encode($options_c); ?>,
select1 = 'select[name="delivery_one"]',
select2 = 'select[name="delivery_two"]';
// Utility function to fill dynamically the select field options
function dynamicSelectOptions( opt ){
var options = '';
$.each( opt, function( key, value ){
options += '<option value="'+key+'">'+value+'</option>';
});
$(select2).html(options);
}
// 1. When dom is loaded we add the select field option for "A" value
// => Disabled (optional) — Uncomment below to enable
// dynamicSelectOptions( opa );
// 2. On live selection event on the first dropdown
$(select1).change(function(){
if( $(this).val() == 'A' )
dynamicSelectOptions( opa );
else if( $(this).val() == 'B' )
dynamicSelectOptions( opb );
else if( $(this).val() == 'C' )
dynamicSelectOptions( opc );
else
dynamicSelectOptions( op0 ); // Reset to default
});
});
</script>
<?php
}
// Check checkout custom fields
add_action( 'woocommerce_checkout_process', 'wps_check_checkout_custom_fields', 20 ) ;
function wps_check_checkout_custom_fields() {
// if custom fields are empty stop checkout process displaying an error notice.
if ( empty($_POST['delivery_one']) || empty($_POST['delivery_two']) ){
$notice = __( 'Please select a day part under Delivery options' );
wc_add_notice( '<strong>' . $notice . '</strong>', 'error' );
}
}
Code goes in function.php file of your active child theme (active theme).
This is a fully working and tested example.

Woocommerce- Blank content on checkout column after inserting a hooked function with get_value

I've been trying to hook this function in one of the "Order Hooks" of the Woocommerce Checkout page:
add_action( 'woocommerce_checkout_before_order_review', 'add_box_conditional' );
function add_box_conditional ( $checkout ) {
woocommerce_form_field( 'test', array(
'type' => 'checkbox',
'class' => array('test form-row-wide'),
'label' => __('conditional test'),
'placeholder' => __(''),
), $checkout->get_value( 'test' ));
}
If i try to get the value of the custom box in any order hooks, the order info just hangs and stops loading. I've tried with another type of custom fields and the same happens.
Example
If I hook the function outside the order contents works perfectly. The custom check box will be used to add a fee (post validation), as it is a very important option for our shop I want it inside the order details, so it can have a strong focus. Is there a way to make the function work on these hooks, or should I put it anywhere and move it with a simple but not so clean CSS overwritte?
You can't just get the value like that $checkout->get_value( 'test' ));.
Hook woocommerce_checkout_create_order and get the value from $_POST there. Then add a custom fee to the order if the checkbox was checked.
Like this:
function add_box_conditional() {
woocommerce_form_field( 'test', array(
'type' => 'checkbox',
'class' => array( 'test form-row-wide' ),
'label' => __( 'conditional test' ),
'placeholder' => __( '' ),
) );
}
add_action( 'woocommerce_checkout_before_order_review', 'add_box_conditional' );
function edit_order( $order, $data ) {
if( ! isset( $_POST[ 'test' ] ) ) {
return;
}
$checkbox_value = filter_var( $_POST[ 'test' ], FILTER_SANITIZE_NUMBER_INT );
if( $checkbox_value ){
$fee = 20;
$item = new \WC_Order_Item_Fee();
$item->set_props( array(
'name' => __( 'Custom fee', 'textdomain' ),
'tax_class' => 0,
'total' => $fee,
'total_tax' => 0,
'order_id' => $order->get_id(),
) );
$item->save();
$order->add_item( $item );
$order->calculate_totals();
}
}
add_action( 'woocommerce_checkout_create_order', 'edit_order', 10, 2 );

Categories