WooCommerce Custom Checkout Fields Based on Product and Quantity - php

I've put together a WooCommerce store that uses variable products to sell tickets to events based on location and date.
When a user adds a ticket to their basket and checks out I'm trying to get a series of custom fields to show based on the number of tickets they have purchased. So for example, the fields I need to capture are:
Title
Name
Organisation
Position
Email
Tel
These will be captured for each person attending the event (i.e. the number of tickets being sold). It also needs to be able to capture this information for each event that's being attended.
I stumbled upon a piece of code on GitHub - https://gist.github.com/joslex/43a5c90af83f9024fd80a71495eac4fc - which seems to hold the answer - the only problem is this part of the code seems to trip it up:
$items = $woocommerce->cart->get_cart();
I've read that it has something to do with calling cart->get_cart(); after wp_loaded but if I want these fields to appear before woocommerce_before_order_notes I'm not sure how to avoid calling cart->get_cart();
Here's the full code from GitHub:
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$i = 1;
foreach($items as $item => $values) {
$_product = $values['data']->post;
$quantity = $values['quantity'];
$x = 1;
while ($x <= $quantity) {
echo '<div class="camper-group"><h3>' . $_product->post_title . __(' - Camper ' . $x . ' Information') .'</h3>';
woocommerce_form_field( 'camper_name_' . $x, array(
'type' => 'text',
'class' => array('camper form-row-wide'),
'label' => __('Camper ' . $x . ' Name'),
'placeholder' => __(''),
), $checkout->get_value( 'camper_name_' . $x ));
woocommerce_form_field( 'camper_grade_' . $x, array(
'type' => 'select',
'class' => array('camper form-row-wide'),
'label' => __('Camper ' . $x . ' Grade'),
'placeholder' => __(''),
'options' => array(
'blank' => __( 'Select a grade', 'wps' ),
'1st' => __( '1st', 'wps' ),
'2nd' => __( '2nd', 'wps' ),
'3rd' => __( '3rd', 'wps' ),
'4th' => __( '4th', 'wps' ),
'5th' => __( '5th', 'wps' ),
'6th' => __( '6th', 'wps' ),
'7th' => __( '7th', 'wps' ),
'8th' => __( '8th', 'wps' ),
'9th' => __( '9th', 'wps' ),
'10th' => __( '10th', 'wps' )
)
), $checkout->get_value( 'camper_grade_' . $x ));
woocommerce_form_field( 'camper_school_' .$x, array(
'type' => 'text',
'class' => array('camper form-row-wide'),
'label' => __('Camper ' . $x . ' School Name'),
'placeholder' => __('The school your child will attend next fall'),
), $checkout->get_value( 'camper_school_' . $x ));
echo '</div>';
$x++;
}
$i++;
}

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/

Updating WooCommerce Admin Shipping Order Fields

In functions.php I am trying to use this, but it is not working:
function rt_woocommerce_admin_shipping_fields( $fields ) {
$fields['first_name']['value'] = $_GET['f'];
$fields['last_name']['value'] = $_GET['l'];
$fields['address_1']['value'] = $_GET['a'];
$fields['address_2']['value'] = $_GET['b'];
// etc
// etc
return $fields;
}
add_filter( 'woocommerce_admin_shipping_fields', 'rt_woocommerce_admin_shipping_fields' );
woocommerce_admin_billing_fields() works but the shipping function does not. Any advice? I need to update the fields with $_GET variables on page load. This works perfectly for the billing fields.
The array has label and show indexes for each item in shipping fields. There is no value index by default. See the woocommerce_admin_shipping_fields filter below.
self::$shipping_fields = apply_filters(
'woocommerce_admin_shipping_fields',
array(
'first_name' => array(
'label' => __( 'First name', 'woocommerce' ),
'show' => false,
),
'last_name' => array(
'label' => __( 'Last name', 'woocommerce' ),
'show' => false,
),
'company' => array(
'label' => __( 'Company', 'woocommerce' ),
'show' => false,
),
'address_1' => array(
'label' => __( 'Address line 1', 'woocommerce' ),
'show' => false,
),
'address_2' => array(
'label' => __( 'Address line 2', 'woocommerce' ),
'show' => false,
),
'city' => array(
'label' => __( 'City', 'woocommerce' ),
'show' => false,
),
'postcode' => array(
'label' => __( 'Postcode / ZIP', 'woocommerce' ),
'show' => false,
),
'country' => array(
'label' => __( 'Country / Region', 'woocommerce' ),
'show' => false,
'type' => 'select',
'class' => 'js_field-country select short',
'options' => array( '' => __( 'Select a country / region…', 'woocommerce' ) ) + WC()->countries->get_shipping_countries(),
),
'state' => array(
'label' => __( 'State / County', 'woocommerce' ),
'class' => 'js_field-state select short',
'show' => false,
),
'phone' => array(
'label' => __( 'Phone', 'woocommerce' ),
),
)
);

Changing the array description text without editing plugin's core files with a filter function in wordpress

I have checked many questions from this forum about this solution, but I could not get it at all. I'm learning how to overwrite some text from existing array in plugin without editing it's core files so I will not loose my changes when the core plugin is updated in wordpress.
What I have is this code right from the plugin. There is one string which I can not translate from po.edit. Please get that I'm just learning this solution so I do not know how to really get it at all so that's why I'm asking for your advice.
Core code
/**
* Initialize integration settings form fields.
*
* #return void
*/
public function init_form_fields() {
$this->form_fields = array(
'authorization' => array(
'title' => __( 'Authorization', 'woocommerce-bookings' ),
'type' => 'google_calendar_authorization',
),
'testing' => array(
'title' => __( 'Connect with a custom Google Calendar App', 'woocommerce-bookings' ),
'type' => 'title',
'description' => 'Enter the credentials below to use a custom Google Calendar API app. Disconnect existing connection to enter credentials.',
),
'client_id' => array(
'title' => __( 'Client ID', 'woocommerce-bookings' ),
'type' => 'text',
'description' => __( 'Enter the Google Client ID associated with your Calendar API app.', 'woocommerce-bookings' ),
'disabled' => $this->is_using_wooconnect_method(),
'desc_tip' => true,
'default' => '',
),
'client_secret' => array(
'title' => __( 'Client Secret', 'woocommerce-bookings' ),
'type' => 'text',
'description' => __( 'Enter the Google Client Secret associated with your Calendar API app.', 'woocommerce-bookings' ),
'disabled' => $this->is_using_wooconnect_method(),
'desc_tip' => true,
'default' => '',
),
'custom_authorization' => array(
'title' => __( 'Authorization', 'woocommerce-bookings' ),
'type' => 'custom_google_calendar_authorization',
),
'calendar_connection_settings' => array(
'title' => __( 'Connected Calendar Settings', 'woocommerce-bookings' ),
'type' => 'title',
'display_check' => array( $this, 'display_connection_settings' ),
),
'calendar_id' => array(
'title' => __( 'Calendar', 'woocommerce-bookings' ),
'type' => 'select',
'description' => __( 'Select your Calendar.', 'woocommerce-bookings' ),
'desc_tip' => true,
'default' => '',
'options' => $this->get_calendar_list_options(),
'display_check' => array( $this, 'display_connection_settings' ),
),
'sync_preference' => array(
'type' => 'select',
'title' => __( 'Sync Preference', 'woocommerce-bookings' ),
'options' => array(
'both_ways' => __( 'Sync both ways - between Store and Google', 'woocommerce-bookings' ),
'one_way' => __( 'Sync one way - from Store to Google', 'woocommerce-bookings' ),
),
'description' => __( 'Manage the sync flow between your Store calendar and Google calendar.', 'woocommerce-bookings' ),
'desc_tip' => true,
'default' => 'one_way',
'display_check' => array( $this, 'display_connection_settings' ),
),
'debug' => array(
'title' => __( 'Debug Log', 'woocommerce-bookings' ),
'type' => 'checkbox',
'label' => __( 'Enable logging', 'woocommerce-bookings' ),
'default' => 'no',
/* translators: 1: log file path */
'description' => sprintf( __( 'Log Google Calendar events, such as API requests, inside %s', 'woocommerce-bookings' ), '<code>woocommerce/logs/' . $this->id . '-' . sanitize_file_name( wp_hash( $this->id ) ) . '.txt</code>' ),
),
);
}
/**
* Generate Settings HTML.
*
* Extends base class html generation to add 'display_check' parameter to each
* field. 'display_check', is a callable that enables/disables the display of
* the field.
*
* #param array $form_fields (default: array()) Array of form fields.
* #param bool $echo Echo or return.
* #return string the html for the settings
* #since 1.15.0
*/
public function generate_settings_html( $form_fields = array(), $echo = true ) {
if ( empty( $form_fields ) ) {
$form_fields = $this->get_form_fields();
}
foreach ( $form_fields as $index => $field ) {
// Delete fields if they have an "enable_check" function that returns false.
if ( isset( $field['display_check'] ) && ! call_user_func( $field['display_check'] ) ) {
unset( $form_fields[ $index ] );
}
}
return parent::generate_settings_html( $form_fields, $echo );
}
Now there is a testing array in the core code where I need to change its description
'testing' => array(
'title' => __( 'Connect with a custom Google Calendar App', 'woocommerce-bookings' ),
'type' => 'title',
'description' => 'Enter the credentials below to use a custom Google Calendar API app. Disconnect existing connection to enter credentials.',
),
I have tried this but I think its not the right solution for my problem because I can not get it work.
add_filter( 'init_form_fields', 'replace_text', 11, 2 );
function replace_text( $form_fields ) {
$form_fields = array(
'testing'=> array(
'title'=> __( 'Connect with a custom Google Calendar App', 'woocommerce-bookings' ),
'type'=> 'title',
'description' => 'New description',
),
);
return $form_fields;
}
Could you please help me to find a right solution. Thank you

Search posts with meta data in Wordpress

I have created a site for study in abroad and i want to create search form for courses in site. I have added meta box for posting, but i cannot solve search form for meta data in posts.
Search form must be:
1) Countries
2) Course Languaes
3) Course Types
4) City of School
This is code in functions.php
if ( ! function_exists( 'thim_add_course_meta' ) ) {
function thim_add_course_meta( $meta_box ) {
$fields = $meta_box['fields'];
$fields[] = array(
'name' => esc_html__( 'Yaş Həddi', 'eduma' ),
'id' => 'thim_course_age',
'type' => 'text',
'desc' => esc_html__( 'yas heddi', 'eduma' ),
'std' => esc_html__( '18', 'eduma' )
);
$fields[] = array(
'name' => esc_html__( 'Dil', 'eduma' ),
'id' => 'thim_course_language',
'type' => 'text',
'desc' => esc_html__( 'Language\'s used for studying', 'eduma' ),
'std' => esc_html__( 'English', 'eduma' )
);
$fields[] = array(
'name' => esc_html__( 'Şəhər', 'eduma' ),
'id' => 'thim_course_city',
'type' => 'text',
'desc' => esc_html__( 'Language\'s used for studying', 'eduma' ),
'std' => esc_html__( 'Şəhər', 'eduma' )
);
$fields[] = array(
'name' => esc_html__( 'Ölkə', 'eduma' ),
'id' => 'thim_course_country',
'type' => 'text',
'desc' => esc_html__( 'Language\'s used for studying', 'eduma' ),
'std' => esc_html__( 'Ölkə', 'eduma' )
);
$meta_box['fields'] = $fields;
return $meta_box;
}
}
And how to i can change meta box type (from text to array)?

Hide Custom Checkout Fields on WooCommerce Based on User Role

I've created a user role of CSR and several custom checkout fields to appear on the Checkout page of WooCommerce, and I want to hide these checkout fields from any other user but those with the CSR role.
I've created the fields and the role, but something is off with my fields as they're still showing up for all users. I followed the tutorial here to hide the fields. Apologies if the formatting of the code is off. The editor didn't accept most of my formatting when I pulled it in from Atom.
ADD CUSTOM FIELDS
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div class="my_custom_checkout_field"><h2>' . __('CSR Information')
.'</h2>';
woocommerce_form_field( 'date_of_purchase', array(
'type' => 'text',
'label' => __('Date of Purchase', 'woocommerce'),
'placeholder' => _x('MM/DD/YYYY', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
), $checkout->get_value( 'date_of_purchase' ));
woocommerce_form_field( 'place_of_purchase', array(
'type' => 'select',
'label' => __('Place of Purchase', 'woocommerce'),
'placeholder' => _x('Select Option', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'options' => array('option-1' => 'Option 1', 'option_2' => 'Option 2',
'option_3' => 'Option 3'),
), $checkout->get_value( 'place_of_purchase' ));
woocommerce_form_field( 'color_item', array(
'type' => 'select',
'label' => __('Product Color', 'woocommerce'),
'placeholder' => _x('Select Option', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'options' => array('option-1' => 'Option 1', 'option_2' => 'Option 2',
'option_3' => 'Option 3'),
), $checkout->get_value( 'color_item' ));
woocommerce_form_field( 'product_model', array(
'type' => 'select',
'label' => __('Model', 'woocommerce'),
'placeholder' => _x('Select Option', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'options' => array('option-1' => 'Option 1', 'option_2' => 'Option 2',
'option_3' => 'Option 3'),
), $checkout->get_value( 'product_model' ));
echo '<strong>' . __('Check All That Apply:') .'</strong>';
woocommerce_form_field( 'lightbulb_out', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Lightbulb is Out'),
'required' => false,
), $checkout->get_value( 'lightbulb_out' ));
woocommerce_form_field( 'not_turn_on', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Will Not Turn On'),
'required' => false,
), $checkout->get_value( 'not_turn_on' ));
woocommerce_form_field( 'fan_not_running', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Fan Stopped Running'),
'required' => false,
), $checkout->get_value( 'fan_not_running' ));
woocommerce_form_field( 'strange_noise', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Strange Noise'),
'required' => false,
), $checkout->get_value( 'strange_noise' ));
woocommerce_form_field( 'not_catching', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Not Catching Insects'),
'required' => false,
), $checkout->get_value( 'not_catching' ));
woocommerce_form_field( 'csr_other', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Other'),
'required' => false,
), $checkout->get_value( 'csr_other' ));
woocommerce_form_field( 'case_description', array(
'type' => 'textarea',
'label' => __('Description of Case', 'woocommerce'),
'placeholder' => _x('Please provide details', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
), $checkout->get_value( 'case_description' ));
echo '</div>';
}
ADD CSR ROLE
$result = add_role( 'csr', __('CSR' ),
array(
'read' => true, // true allows this capability
'edit_posts' => false, // Denies user to edit their own posts
'edit_pages' => false, // Denies user to edit pages
'edit_others_posts' => false, // Denies user to edit others posts not just
their own
'create_posts' => false, // Denies user to create new posts
'manage_categories' => false, // Denies user to manage post categories
'publish_posts' => false, // Denies the user to publish, otherwise posts stays
in draft mode
'edit_themes' => false, // false denies this capability. User can’t edit your
theme
'install_plugins' => false, // User cant add new plugins
'update_plugin' => false, // User can’t update any plugins
'update_core' => false // user cant perform core updates
)
);
HIDE CSR VALUES FOR ALL BUT CSR
function custom_override_checkout_fields( $fields ) {
if ( ! current_user_can( 'csr' ) && isset( $fields['date_of_purchase'] ) ) {
unset( $fields[['date_of_purchase']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['place_of_purchase'] ) ) {
unset( $fields[['place_of_purchase']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['color_item'] ) ) {
unset( $fields[['color_item']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['product_model'] ) ) {
unset( $fields[['product_model']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['lightbulb_out'] ) ) {
unset( $fields[['lightbulb_out']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['not_turn_on'] ) ) {
unset( $fields[['not_turn_on']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['fan_not_running'] ) ) {
unset( $fields[['fan_not_running']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['strange_noise'] ) ) {
unset( $fields[['strange_noise']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['not_catching'] ) ) {
unset( $fields[['not_catching']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['csr_other'] ) ) {
unset( $fields[['csr_other']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['case_description'] ) ) {
unset( $fields[['case_description']] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields'
);
I've expanded on the code I posted in my tutorial on how to customize the WooCommerce Checkout.
First you need to register the new checkout fields. This is where I've added a current_user_can() to test if the current user has the appropriate capabilities to see these extra fields. You can probably use current_user_can( 'csr' ) or even better add something like a manage_csr capability to the csr role. I'm using the manage_options capability because it was easier for me to test.
// Add new checkout fields
function kia_filter_checkout_fields( $fields ){
if( current_user_can( 'manage_options' ) ) {
$fields['extra_fields'] = array(
'date_of_purchase' => array(
'type' => 'text',
'label' => __( 'Date of Purchase', 'your-plugin' ),
'placeholder' => _x ('MM/DD/YYYY', 'placeholder', 'your-plugin' ),
'required' => false,
'class' => array( 'form-row-wide' ),
'clear' => true
),
'place_of_purchase' => array(
'type' => 'select',
'label' => __( 'Place of Purchase', 'your-plugin' ),
'placeholder' => _x( 'Select Option', 'placeholder', 'your-plugin' ),
'required' => false,
'class' => array('form-row-wide' ),
'clear' => true,
'options' => array('option-1' => __( 'Option 1', 'your-plugin' ), 'option_2' => __( 'Option 2', 'your-plugin' ), 'option_3' => __( 'Option 3', 'your-plugin' ) )
),
'color_item' => array(
'type' => 'select',
'label' => __( 'Product Color', 'your-plugin' ),
'placeholder' => _x( 'Select Option', 'placeholder', 'your-plugin' ),
'required' => false,
'class' => array( 'form-row-wide' ),
'clear' => true,
'options' => array( 'option-1' => __( 'Option 1', 'your-plugin' ), 'option_2' => __( 'Option 2', 'your-plugin' ), 'option_3' => __( 'Option 3', 'your-plugin' ) )
),
'product_model' => array(
'type' => 'select',
'label' => __( 'Model', 'your-plugin' ),
'placeholder' => _x('Select Option', 'placeholder', 'your-plugin' ),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'options' => array( 'option-1' => __( 'Option 1', 'your-plugin' ), 'option_2' => __( 'Option 2', 'your-plugin' ), 'option_3' => __( 'Option 3', 'your-plugin' ) )
),
'product_condition' => array(
'type' => 'multicheck',
'label' => __( 'Product Condition:', 'your-plugin' ),
'description' => __( 'Check All That Apply:', 'your-plugin' ),
'required' => false,
'clear' => true,
'options' => array( 'lightbulb_out' => __( 'Lightbulb is Out', 'your-plugin' ),
'not_turn_on' => __( 'Will Not Turn On', 'your-plugin' ),
'fan_not_running' => __( 'Fan Stopped Running', 'your-plugin' ),
'strange_noise' => __( 'Strange Noise', 'your-plugin' ),
'not_catching' => __( 'Not Catching Insectsn', 'your-plugin' ),
'csr_other' => __( 'Other', 'your-plugin' ),
),
),
'case_description' => array(
'type' => 'textarea',
'label' => __( 'Description of Case', 'your-plugin' ),
'placeholder' => _x( 'Please provide details', 'placeholder', 'your-plugin' ),
'required' => false,
'class' => array( 'form-row-wide' ),
'clear' => true,
),
);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );
You will note that I've done something different with your "Fan Stopped", etc checkboxes. You don't have to do this, but I was curious and procrastinating. WooCommerce doesn't support a multi-check set of checkboxes, but it does support defining your own custom field types. So with the following we create a new type of form field:
function kia_multicheck_form_field( $field, $key, $args, $value ){
$field_html = '<fieldset>';
if( isset( $args['label'] ) ){
$field_html .= '<legend>' . $args['label'] . '</legend>';
}
if ( ! empty( $args['options'] ) ) {
foreach ( $args['options'] as $option_key => $option_text ) {
$field_html .= '<input type="checkbox" class="input-multicheck ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" value="' . esc_attr( $option_key ) . '" name="' . esc_attr( $key ) . '[]" id="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '"' . checked( $value, $option_key, false ) . ' />';
$field_html .= '<label for="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '" class="multicheck ' . implode( ' ', $args['label_class'] ) . '">' . $option_text . '</label>';
}
}
if ( $args['description'] ) {
$field_html .= '<span class="description">' . esc_html( $args['description'] ) . '</span>';
}
$field_html .= '</fieldset>';
$container_class = esc_attr( implode( ' ', $args['class'] ) );
$container_id = esc_attr( $args['id'] ) . '_field';
$after = ! empty( $args['clear'] ) ? '<div class="clear"></div>' : '';
$field_container = '<p class="form-row %1$s" id="%2$s" data-sort="' . esc_attr( $sort ) . '">%3$s</p>';
$field = sprintf( $field_container, $container_class, $container_id, $field_html ) . $after;
return $field;
}
add_filter( 'woocommerce_form_field_multicheck', 'kia_multicheck_form_field', 10, 4 );
Next we display the new fields on the checkout page.... but only if they exist, because harkening back to the first code block, they won't be in the checkout fields array if the user doesn't have the right permissions.
// display the extra field on the checkout form
function kia_extra_checkout_fields(){
$checkout = WC()->checkout();
if( isset( $checkout->checkout_fields['extra_fields'] ) ) { ?>
<div class="extra-fields">
<h3><?php _e( 'CSR Information' ); ?></h3>
<?php
// because of this foreach, everything added to the array in the previous function will display automagically
foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) :
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
endforeach; ?>
</div>
<?php }
}
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
Now you'll probably need to add some CSS to make this look better, and you need a way to save the multicheck data. You can review my tutorial on how to save the rest, and perhaps figure out the multicheck on your own.

Categories