Add a step to WooCommerce Checkout - wordpress

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' );

Related

Add a link to the product page woocomerce

I have made a custom field in the product card and displayed it in the catalogue, but the problem is that there is no link to go to the product card. I would appreciate any help.
Here is the code for adding a field in the product card
// Field Short name
add_action( 'woocommerce_product_options_general_product_data', 'shorttitle_custom_general_fields' );
add_action( 'woocommerce_process_product_meta', 'shorttitle_custom_general_fields_save' );
function shorttitle_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => 'shorttitle_field',
'label' => __( 'Краткое название', 'woocommerce' ),
//'desc_tip' => 'true',
//'description' => __( 'Краткое название', 'woocommerce' ),
'type' => 'html'
)
);
echo '</div>';
}
function shorttitle_custom_general_fields_save( $post_id ){
// Text Field
$woocommerce_text_field = $_POST['shorttitle_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, 'shorttitle_field', esc_attr( $woocommerce_text_field ) );
}
This is the code for displaying this field on the catalogue page
// Change the output of the product header (in the catalogue)
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title' );
add_action( 'woocommerce_shop_loop_item_title', 'custom_woocommerce_template_loop_product_title', 99 );
function custom_woocommerce_template_loop_product_title() {
global $post;
$product = wc_get_product( $post->ID );
$title = get_post_meta( $post->ID, 'shorttitle_field', true );
if( $title ) {
echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $title ) . '</h2>';
} else {
echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $product->get_title() ) . '</h2>';
}
}
Page https://www.krisstyle.com.ua/shop/

Display content on single product page in Woocommerce based on category

I'm sooooo close - but I can't get this last little bit.
I am adding the authors name to books we have for sale in woo. The style needs to be different from the book title so I'm using a custom meta field to put it under the title and style it the way I want. I have everything working and where I want it, now I just need for it only show on a particular category instead of on all products.
There seems to me to be two ways to do this.
1 - Only display it for products in a particular category
2 - Only display it if there is content.
I'm currently trying to only display if it's a particular category, but while writing this, its seems a more elegant solution to only display if content exists.
Here's what I have
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
if ( is_product() && has_term( 'books-media', 'product_cat' ) ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
This doesn't display anywhere, but when I take out the if statement it shows on all products.
Any idea where I went awry?
Here is the answer in context Helga - this snippet will add a custom meta field to the woocommerce product editing dashboard, save the content and display it under the product title on the single product view.
/* add product subhead custom field to woocommerce product dashboard */
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'MY-LABEL', 'woocommerce' ),
'placeholder' => 'Author',
'desc_tip' => 'true',
'description' => __( 'This text will show below the product title.', 'woocommerce' )
)
);
echo '</div>';
}
/* Save woo custom field */
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_text_field = $_POST['_text_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
}
/* add author below title on single product */
function cn_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_subhead_to_title', 6 );
What if you simply test for the presence of the meta? If you don't save the meta in the other categories it will never display:
function cn_add_add_subhead_to_title() {
global $post;
if ( $meta = get_post_meta( $post->ID, '_text_field', true ) ) {
?>
<div class="cn-subhead">
<?php printf( __( 'by %s', 'textdomain' ), $meta ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
Helga - Thanks for your interest and help. I had a little time so I decided to try the other route of only displaying the meta if there was content instead of based on category. This may be a more flexible solution anyway, leaving the field open to any sort of subhead they want instead of limiting it only to Authors.
This the code that worked:
/* add author below title on single product */
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );

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

Time Picker for My WooCommerce Checkout Form

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>';
}

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