Time Picker for My WooCommerce Checkout Form - woocommerce

Does anyone know of any code I can add to my theme so that my checkout form includes a field for picking a delivery time? My website is monpetitfour.com
EDIT:
So, I found coding that works in terms of outputting the time options for my customers to choose from, but I'm wondering how I can get the option they choose to be recorded in the woocommerce "thank you" page and the customer & admin new order emails? Here's the code:
<p class="form-row my-field-class form-row-wide woocommerce-validated"
id="time_slot_field">
<label for="time_slot" class="">Choose a Delivery Time</label>
<select name="time_slot" id="time_slot" class="select" required="true">
<option value="">Select</option>
<option value="07:00 AM - 07:30 AM">07:00 AM - 07:30 AM</option>
<option value="07:30 PM - 08:00 AM">07:30 AM - 08:00 AM</option>
<option value="08:00 AM - 08:30 AM">08:00 AM - 08:30 AM</option>
....
</select></p>

As described in WooCommerce Customize Checkout Fields here's how you create a custom checkout field:
// Add a new checkout field
function kia_filter_checkout_fields($fields){
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );
// display the extra field on the checkout form
function kia_extra_checkout_fields(){
$checkout = WC()->checkout(); ?>
<div class="extra-fields">
<h3><?php _e( 'Additional Fields' ); ?></h3>
<?php foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>
<?php }
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
if( isset( $posted['some_field'] ) ) {
update_post_meta( $order_id, '_some_field', sanitize_text_field( $posted['some_field'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 2 );
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( 'Extra Details', 'woocommerce' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_some_field', true ) . '</p>'; ?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );

I used HelgatheViking's code with a few tweaks for a dropdown option so I'm just posting that first section of the code "//add a new checkout field" here in case anyone else wants to add a delivery time option to their checkout:
// Add a new checkout field
function kia_filter_checkout_fields($fields){
$fields['extra_fields'] = array(
'some_field' => array(
'required' => true,
'label' => __( 'Delivery Time' ),
'type' => 'select',
'options' => array(
'7:00AM-7:30AM' => __('7:00 AM - 7:30 AM', 'woocommerce' ),
'7:30AM-8:00AM' => __('7:30 AM - 8:00 AM', 'woocommerce' ),
....
'5:30PM-6:00PM' => __('5:30 PM - 6:00 PM', 'woocommerce' )
)
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );
(P.S.) Make sure you check out her site for the code you need to add the resulting delivery time (or field value) to your customer & admin new order emails.
UPDATE: To add the delivery time to the order details page (the thankyou.php template) using all the example of all the coding mentioned here in this thread, simply add the following code to your theme's functions.php file:
add_action( 'woocommerce_order_details_after_order_table', 'nolo_custom_field_display_cust_order_meta', 10, 1 );
function nolo_custom_field_display_cust_order_meta($order){
echo '<p><strong>'.__('Delivery Time').':</strong> ' . get_post_meta( $order->id, '_some_field', true ). '</p>';
}

Related

Can't add content to woocommerce_after_shop_loop

I'm trying to add a second product description to my WooCommerce page: https://alvinecph.dk/kategori/to-go/ from the guide from https://www.businessbloomer.com/woocommerce-add-a-second-content-box-product-category-pages/ .
I do however experience an issue, that the second description isn't showing for the hook woocommerce_after_shop_loop. I've tried to change the hook to woocommerce_before_shop_loop_item, and this seems to work, showing the text. So it could seem like woocommerce_after_shop_loop isn't present on the page.
I'm using the Divi theme, and have created a template for the category/archive page with their builder. To this template I've the Woo Product block. So I'm really baffled about this.
Is there something I'm missing. I've also tried to disable all plugins.
BR
Martin
// 1. Display field on "Add new product category" admin page
add_action( 'product_cat_add_form_fields', 'bbloomer_wp_editor_add', 10, 2 );
function bbloomer_wp_editor_add() {
?>
<div class="form-field">
<label for="seconddesc"><?php echo __( 'Second Description', 'woocommerce' ); ?></label>
<?php
$settings = array(
'textarea_name' => 'seconddesc',
'quicktags' => array( 'buttons' => 'em,strong,link' ),
'tinymce' => array(
'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
'theme_advanced_buttons2' => '',
),
'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
);
wp_editor( '', 'seconddesc', $settings );
?>
<p class="description"><?php echo __( 'This is the description that goes BELOW products on the category page', 'woocommerce' ); ?></p>
</div>
<?php
}
// ---------------
// 2. Display field on "Edit product category" admin page
add_action( 'product_cat_edit_form_fields', 'bbloomer_wp_editor_edit', 10, 2 );
function bbloomer_wp_editor_edit( $term ) {
$second_desc = htmlspecialchars_decode( get_woocommerce_term_meta( $term->term_id, 'seconddesc', true ) );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="second-desc"><?php echo __( 'Second Description', 'woocommerce' ); ?></label></th>
<td>
<?php
$settings = array(
'textarea_name' => 'seconddesc',
'quicktags' => array( 'buttons' => 'em,strong,link' ),
'tinymce' => array(
'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
'theme_advanced_buttons2' => '',
),
'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
);
wp_editor( $second_desc, 'seconddesc', $settings );
?>
<p class="description"><?php echo __( 'This is the description that goes BELOW products on the category page', 'woocommerce' ); ?></p>
</td>
</tr>
<?php
}
// ---------------
// 3. Save field # admin page
add_action( 'edit_term', 'bbloomer_save_wp_editor', 10, 3 );
add_action( 'created_term', 'bbloomer_save_wp_editor', 10, 3 );
function bbloomer_save_wp_editor( $term_id, $tt_id = '', $taxonomy = '' ) {
if ( isset( $_POST['seconddesc'] ) && 'product_cat' === $taxonomy ) {
update_woocommerce_term_meta( $term_id, 'seconddesc', esc_attr( $_POST['seconddesc'] ) );
}
}
// ---------------
// 4. Display field under products # Product Category pages
add_action( 'woocommerce_after_shop_loop', 'bbloomer_display_wp_editor_content', 5 );
function bbloomer_display_wp_editor_content() {
if ( is_product_taxonomy() ) {
$term = get_queried_object();
if ( $term && ! empty( get_woocommerce_term_meta( $term->term_id, 'seconddesc', true ) ) ) {
echo '<p class="term-description">' . wc_format_content( htmlspecialchars_decode( get_woocommerce_term_meta( $term->term_id, 'seconddesc', true ) ) ) . '</p>';
}
}
}

Add image upload custom field at dokan template new-product.php

I am using dokan plugin for a multivendor website, I want to add image upload custom field in the template
new-product.php, I used CMB2 plugin to create image upload custom field with WooCommerce like this
function themebox_metaboxes() {
// Start with an underscore to hide fields from custom fields list
$prefix = 'themebox_met_';
// Product Settings
$header_settings = new_cmb2_box( array(
'id' => 'Extra_settings',
'title' => esc_html__( 'Extra Settings', 'themebox' ),
'object_types' => array( 'product'), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
) );
$header_settings->add_field( array(
'name' => esc_html__( 'Add Image Detail size 590x300 px', 'themebox' ),
'id' => $prefix . 'img_detail',
'type' => 'file'
) );
}
I want to add this custom image upload field in template form new-product.php and when save form in dokan
the image upload custom field update with an image added in dokan .....exactly like featured product image in WooCommerce
You can override the new-product.php and new-product-single.php file and then add your field on the product upload/edit file. To add new field for product edit/add the template you will have to add a new field in this template (Override via child-theme) - dokan-lite/templates/products/new-product-single.php. However, there are some other steps required to successfully save them.
You need to modify the Dokan product upload template and then you have to add an extra field by overriding the template. After adding the input filed you have to save the value of the field. On that place you have to use do_action( 'dokan_new_product_added', $product_id, $post_data ); this hook to save the field data.
When you will edit the product that time you have to use do_action( 'dokan_product_updated', $post_id ); to re-save.
Thanks :)
function save_add_product_meta($product_id, $postdata){
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
if ( ! empty( $postdata['title'] ) ) {
update_post_meta( $product_id, 'title', $postdata['title'] );
}
if ( ! empty( $postdata['subtitle'] ) ) {
update_post_meta( $product_id, 'subtitle', $postdata['subtitle'] );
}
if ( ! empty( $postdata['subdescription'] ) ) {
update_post_meta( $product_id, 'subdescription', $postdata['subdescription'] );
}
if ( ! empty( $postdata['vidimg'] ) ) {
update_post_meta( $product_id, 'vidimg', $postdata['vidimg'] );
}
}
/*
* Showing field data on product edit page
*/
add_action('dokan_product_edit_after_product_tags','show_on_edit_page',99,8);
function show_on_edit_page($post, $post_id){
$subtitle = get_post_meta( $post_id, 'subtitle', true );
$title = get_post_meta( $post_id, 'title', true );
$subdesc = get_post_meta( $post_id, 'subdescription', true );
$vidimg = get_post_meta( $post_id, 'vidimg', true );
?>
<div class="dokan-form-group">
<h6 class="auto">Ajoutez du contenu pour mettre en valeur cette oeuvre !</h6>
<input type="hidden" name="title" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="new_field" class="form-label"><?php esc_html_e( 'Autre Titre', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'title', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $title ) ); ?>
<p class="help-block">50 caractères maximum (conseillé)</p>
</div>
<div class="dokan-form-group">
<input type="hidden" name="subtitle" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="subtitle" class="form-label"><?php esc_html_e( 'Sous titre', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'subtitle', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $subtitle ) ); ?>
<p class="help-block">80 caractères maximum (conseillé)</p>
</div>
<div class="dokan-form-group">
<label for="subdescription" class="form-label">Paragraphe d'introduction</label>
<div class="dokan-rich-text-wrap">
<?php dokan_post_input_box( $post_id, 'subdescription', array('placeholder' => 'ajouter une description', 'value' => $subdesc ), 'textarea' ); ?>
</div>
</div>
<div class="dokan-feat-image-upload">
<?php
$wrap_class = ' dokan-hide';
$instruction_class = '';
$feat_image_id = 0;
if (!empty($vidimg) ) {
$wrap_class = '';
$instruction_class = ' dokan-hide';
$imaid =attachment_url_to_postid($vidimg);
}
?>
<div class="instruction-inside<?php echo esc_attr( $instruction_class ); ?>">
<input type="hidden" name="vidimg" class="dokan-feat-image-id" value="<?php echo esc_attr($vidimg ); ?>">
<i class="fa fa-cloud-upload"></i>
<?php esc_html_e( 'Upload a product cover image', 'dokan-lite' ); ?>
</div>
<div class="image-wrap<?php echo esc_attr( $wrap_class ); ?>">
<a class="close dokan-remove-feat-image">×</a>
<?php if ( ! empty($vidimg) ) { ?>
<img src="<?php echo esc_url(wp_get_attachment_url($vidimg ) ); ?>" alt="">
<?php } else { ?>
<img height="" width="" src="" alt="">
<?php } ?>
</div>
</div><!-- .dokan-feat-image-upload -->
<?php
}

How to arrange the custom field on woocommerce checkout page

I know how to add a custom field to the checkout page, and then how to process the field also.
But when I add a custom field to my form, it always appears at the end of the form. How can I make it appear on top of other fields?
My current script:
<?php *// Add a new checkout field
function kia_filter_checkout_fields($fields){
$fields['extra_fields'] = array(
'message_field' => array(
'type' => 'textarea',
'required' => true,
'label' => __( 'Message Field' )
),
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );
// display the extra field on the checkout form
function kia_extra_checkout_fields(){
$checkout = WC()->checkout(); ?>
<div class="extra-fields">
<h3><?php _e( 'WRITE A MESSAGE TO RECIPIENT' ); ?><span>(<?php _e( 'Leave blank if not required' ); ?>)</span></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 ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>
<?php }
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
// don't forget appropriate sanitization if you are using a different field type
if( isset( $posted['message_field'] ) ) {
update_post_meta( $order_id, '_message_field', sanitize_text_field( $posted['message_field'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 1);
// display the extra data on order recieved page and my-account order review
function kia_display_order_data( $order_id ){ ?>
<h2><?php _e( 'Additional Info' ); ?></h2>
<table class="shop_table shop_table_responsive additional_info">
<tbody>
<tr>
<th><?php _e( 'message_field:' ); ?></th>
<td><?php echo get_post_meta( $order_id, '_message_field', true ); ?></td>
</tr>
<tr>
</tr>
</tbody>
</table>
<?php }
add_action( 'woocommerce_thankyou', 'kia_display_order_data', 20 );
add_action( 'woocommerce_view_order', 'kia_display_order_data', 20 );
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( 'Extra Details', 'woocommerce' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_message_field', true ) . '</p>';
// echo '<p><strong>' . __( 'Another field' ) . ':</strong>' . get_post_meta( $order->id, '_another_field', true ) . '</p>'; ?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );
?>*
Instead of hooking into woocommerce_checkout_after_customer_details you need to hook into woocommerce_checkout_before_customer_details, that way your custom field will appear on top of other fields.
So change the following line of code
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
to
add_action( 'woocommerce_checkout_before_customer_details' ,'kia_extra_checkout_fields' );
Please try to remove class="extra-fields"
<div id="my_custom_checkout_field">
<h3><?php _e( 'WRITE A MESSAGE TO RECIPIENT' ); ?><span>(<?php _e( 'Leave blank if not required' ); ?>)</span></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 ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>
I have used it with id and it works well for me. Like this

Add a step to WooCommerce Checkout

Is there any way to add a new step to the Checkout process in WooCommerce? I need something between the cart and the Billing Details page to collect some additional information. I've researched quite a bit and turned up nothing. I can't be the only person who wants to do this.
From my tutorial on WooCommerce Custom Checkout Fields this is how to add a custom checkout field which you could use to collect additional information.
// Add a new checkout field
function kia_filter_checkout_fields($fields){
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );
// display the extra field on the checkout form
function kia_extra_checkout_fields(){
$checkout = WC()->checkout(); ?>
<div class="extra-fields">
<h3><?php _e( 'Additional Fields' ); ?></h3>
<?php foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>
<?php }
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
if( isset( $posted['some_field'] ) ) {
update_post_meta( $order_id, '_some_field', sanitize_text_field( $posted['some_field'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 2 );
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( 'Extra Details', 'woocommerce' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_some_field', true ) . '</p>'; ?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );

Wordpress/WooCommerce Registration

I'm using a plugin (User Profiles Made Easy) that allows a user to choose their own role when registering on my website. It does a good job with that, but what it doesn't do is allow me to conditionally show/hide other fields based on the role they choose.
I thought of Gravity Form and their User Registration add-on, but it won't allow the user to choose their own role when registering.
The WooCommerce tie-in is this: When the buyer checks out, I could collect the extra data at that time (instead of during registration), but I would want to only show the fields relevant to their chosen role. Any ideas?
Borrowing from my tutorial on cutomizing WooCommerce checkout I think we can just wrap certain fields role/capability conditional logica via current_user_can().
// Add a new checkout field
function kia_filter_checkout_fields($fields){
if( current_user_can('some_capability' ) ){
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
)
);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );
// display the extra field on the checkout form
function kia_extra_checkout_fields(){
if( current_user_can('some_capability' ) ){
$checkout = WC()->checkout(); ?>
<div class="extra-fields">
<h3><?php _e( 'Additional Fields' ); ?></h3>
<?php foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
<?php } ?>
</div>
<?php }
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
if( current_user_can( 'some_capability' && isset( $posted['some_field'] ) ) {
update_post_meta( $order_id, '_some_field', sanitize_text_field( $posted['some_field'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 2 );
// display the extra data on order recieved page and my-account order review
function kia_display_order_data( $order_id ){
if( current_user_can('some_capability' ) ){
?>
<h2><?php _e( 'Additional Info' ); ?></h2>
<table class="shop_table shop_table_responsive additional_info">
<tbody>
<tr>
<th><?php _e( 'Some Field:' ); ?></th>
<td data-title="Telephone"><?php echo get_post_meta( $order_id, '_some_field', true ); ?></td>
</tr>
</tbody>
</table>
<?php }
}
add_action( 'woocommerce_thankyou', 'kia_display_order_data', 20 );
add_action( 'woocommerce_view_order', 'kia_display_order_data', 20 );
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){
if( current_user_can('some_capability' ) ){
?>
<div class="order_data_column">
<h4><?php _e( 'Extra Details', 'woocommerce' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_some_field', true ) . '</p>'; ?>
</div>
<?php }
}
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );

Resources