2024 – Add backorder checkbox to WooCommerce checkout page and save checkbox value to customers order

WooCommerce backorders can be a handy thing to have as they can allow you to continue selling while you await the arrival of more stock. unfortunately though, not all customers know what the term backorder means, and some may miss the notice entirely, if it’s even displayed. This can create frustration for both the customer and store owner. The following pieces of code are designed to solve this problem by placing a checkbox on the checkout page and saving the customers acceptance on the order. It further helps the store by adding an additional column to the orders list screen showing which orders contain backordered items. Let’s get started.

Step 1. Check if the cart contains backordered items. We do this by looping through each item in the cart to determine if an item is on backorder. Notice with the if statement that we are not using curly brackets {}? PHP permits this if the following line is indented or it continues on the same line.

/**
 * Snippet: 2024 – Add backorder checkbox to WooCommerce checkout page and save checkbox value to customers order
* Author: John Cook
* URL: https://wcsuccessacademy.com/?646
* Tested with WooCommerce 8.8.3
* "Loop through the contents of the cart to find products with a backordered item"
*/ function wcsuccess_check_cart_has_backorder_product() { // Loop through each cart item foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { $cart_product = wc_get_product( $values['data']->get_id() ); // Check if product is on backorder and return true if it is if( $cart_product->is_on_backorder() ) return true; } return false; }

Step 2. Add a custom checkout field. This places a required checkbox just before the pay button. If the customer attempts to checkout without accepting it, they will receive an error message

/**
 * Snippet: 2024 – Add backorder checkbox to WooCommerce checkout page and save checkbox value to customers order
* Author: John Cook
* URL: https://wcsuccessacademy.com/?646
* Tested with WooCommerce 8.8.3
* "Add custom checkout field location: woocommerce_review_order_before_submit"
*/ add_action( 'woocommerce_review_order_before_submit', 'wcsuccess_custom_checkout_backorder_field' ); function wcsuccess_custom_checkout_backorder_field() { // Check if cart contains backordered items if(wcsuccess_check_cart_has_backorder_product()){ // If "true" create a checkbox echo '<div id="my_custom_checkout_field">'; woocommerce_form_field( 'backorder_cart_acknowledge', array( 'type' => 'checkbox', // checkbox 'class' => array('input-checkbox'), //checkbox CSS class - modify as necessary 'label' => __('Acknowledge cart contains backorder items'), // Label 'required' => true, // change to false if you do not want it to be a required field ), WC()->checkout->get_value( 'backorder_cart' ) ); // set the name attribute to save to the order echo '</div>'; } }

Step 3. Next, we need to process the checkout and throw the exception if the customer didn’t check the checkbox

/**
 * Snippet: 2024 – Add backorder checkbox to WooCommerce checkout page and save checkbox value to customers order
* Author: John Cook
* URL: https://wcsuccessacademy.com/?646
* Tested with WooCommerce 8.8.3
* "Process the checkout"
*/ add_action('woocommerce_checkout_process', 'wcsuccess_custom_checkout_backorder_field_process'); function wcsuccess_custom_checkout_backorder_field_process() { if(my_check_cart_has_backorder_product()){ // Check if set, if its not set add an error. if ( ! $_POST['backorder_cart_acknowledge'] ) wc_add_notice( __( 'Please acknowledge that your cart contains backordered items and shipping will be delayed' ), 'error' ); } }

Step 4. Save the acceptance to the order and also save the cart as containing a backordered item. This serves two purposes. First, it records the customers action in accepting the item is on backorder. Second, it saves the cart as containing a backordered item which will be displayed on the order list screen. Notice the two meta fields? One is for customer acknowledgement and the other is for the cart.

/**
 * Snippet: 2024 – Add backorder checkbox to WooCommerce checkout page and save checkbox value to customers order
* Author: John Cook
* URL: https://wcsuccessacademy.com/?646
* Tested with WooCommerce 8.8.3
* "Save the custom checkout checkbox field as the order meta and save an additional field that the cart contains backordered items. This will be used on the order list screen"
*/ add_action( 'woocommerce_checkout_create_order', 'wcsuccess_custom_checkout_backorder_field_update_order_meta', 10, 2 ); function wcsuccess_custom_checkout_backorder_field_update_order_meta( $order, $data ) { // Check cart contains backordered items if(my_check_cart_has_backorder_product()){ $value = isset($_POST['backorder_cart_acknowledge']) ? 'yes' : 'no'; // Set the correct values // Save as custom order meta data that shopper has acknowledged the cart contains backorders $order->update_meta_data( 'backorder_cart_acknowledge', $value ); // Save value to the order that it contains backordered items $order->update_meta_data( 'backorder_cart', $value ); } }

Step 5. Display the acceptance below the billing details on the order edit screen

/**
 * Snippet: 2024 – Add backorder checkbox to WooCommerce checkout page and save checkbox value to customers order
* Author: John Cook
* URL: https://wcsuccessacademy.com/?646
* Tested with WooCommerce 8.8.3
* "Displaying the terms field value in the order edit page"
*/ add_action( 'woocommerce_admin_order_data_after_billing_address', 'wcsuccess_backorder_field_display_admin_order_meta', 10, 1 ); function wcsuccess_backorder_field_display_admin_order_meta( $order ){ // Get the custom meta field from the order $accepted_terms = get_post_meta( $order->get_id(), 'backorder_cart_acknowledge', true ); if($accepted_terms != 'no' && !empty( $accepted_terms ) ){ // Check that the field is not empty to display the necessary message if( ! empty( $accepted_terms )) echo '<p><strong>'.__('Accepted backorder', 'woocommerce').':</strong> ' . ucfirst($accepted_terms) . '</p>'; } }

Step 6. Add the acceptance to the order email

/**
 * Snippet: 2024 – Add backorder checkbox to WooCommerce checkout page and save checkbox value to customers order
* Author: John Cook
* URL: https://wcsuccessacademy.com/?646
* Tested with WooCommerce 8.8.3
* "Add a custom field (in an order) to the emails"
*/ add_filter( 'woocommerce_email_order_meta_fields', 'wcsuccess_backorder_woocommerce_email_order_meta_fields', 10, 3 ); function wcsuccess_backorder_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) { // Get the order meta for the terms $accepted_terms = get_post_meta( $order->id, 'backorder_cart_acknowledge', true ); // Only display if the terms field is not empty if( !empty( $accepted_terms ) && $accepted_terms != 'no') { $fields['meta_key'] = array( 'label' => __( 'Accepted cart contains backordered items' ), 'value' => ucfirst($accepted_terms), ); return $fields; } }

Step 7. Add a backorder column to the order list screen

/**
 * Snippet: 2024 – Add backorder checkbox to WooCommerce checkout page and save checkbox value to customers order
* Author: John Cook
* URL: https://wcsuccessacademy.com/?646
* Tested with WooCommerce 8.8.3
* "Add a backorder column to the order list screen"
*/ add_filter( 'manage_edit-shop_order_columns', 'wcsuccess_add_backorder_column_header', 20 ); function wcsuccess_add_backorder_column_header( $columns ) { $new_columns = array(); foreach ( $columns as $column_name => $column_info ) { $new_columns[ $column_name ] = $column_info; if ( 'order_total' === $column_name ) { $new_columns['backorder_cart'] = __( 'Backordered', 'my-textdomain' ); } } return $new_columns; }

Step 8. Add the content of the backorder carts to the columns. Note. This uses the cart backorder and not the acceptance meta field. It will display a yes or no if the order contains backordered items

/**
 * Snippet: 2024 – Add backorder checkbox to WooCommerce checkout page and save checkbox value to customers order
* Author: John Cook
* URL: https://wcsuccessacademy.com/?646
* Tested with WooCommerce 8.8.3
* "Add 'Backorder' column content to 'Orders' page."
* * @param string[] $column name of column being displayed */ add_action( 'manage_shop_order_posts_custom_column', 'wcsuccess_backorder_column_content' ); function wcsuccess_backorder_column_content( $column ) { global $post; if ( 'backorder_cart' === $column ) { $company_name = !empty(get_post_meta($post->ID,'backorder_cart',true)) ? get_post_meta($post->ID,'backorder_cart',true) : 'No'; echo ucfirst($company_name); } }

Step 9. Make the column sortable. This will allow you to sort by backorder, ordering the carts by backorder contents.

/**
 * Snippet: 2024 – Add backorder checkbox to WooCommerce checkout page and save checkbox value to customers order
* Author: John Cook
* URL: https://wcsuccessacademy.com/?646
* Tested with WooCommerce 8.8.3
* "Make custom column sortable"
*/ add_filter( "manage_edit-shop_order_sortable_columns", 'wcsuccess_shop_order_column_meta_field_sortable' ); function wcsuccess_shop_order_column_meta_field_sortable( $columns ){ $meta_key = 'backorder_cart'; return wp_parse_args( array('backorder_cart' => $meta_key), $columns ); }

Add all of these snippets to the child theme functions,php file or a code snippets plugin.

See also  How to Set Minimum Order Amount in WooCommerce - 2024

Did this work for you? If it worked and has made your life easier, please leave a comment.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
nick
nick
July 22, 2023 6:41 am

Get an error at checkout. Critical error when step 2 code is activated. It just can’t show the checkbox.

Richard H
Richard H
October 4, 2023 10:08 pm

I too still get an error on step 2, once all code is in, no tick box at checkout, the rest seems good.

Alicia
Alicia
March 27, 2024 6:58 pm

Thank you! This was very helpful.

5
0
Would love your thoughts, please comment.x
()
x
Scroll to Top
×