This post will discuss the what, the why and the how of modifying a WooCommerce Order status by simply scanning a QR code.
What are WooCommerce Order statuses? What is a QR code?
WooCommerce has a number of status built in when an order is placed, from Pending Payment, through to Processing or Failed, through to Completed, On-hold or Cancelled. These statuses help e-commerce store owners track what needs to be done to fulfil the order.
A QR code is a type of barcode. QR codes most often contain information to open web pages, any normal smartphone camera can scan the code and quickly open the link.
Why update WooCommerce Order statuses?
Many store owners with offline sales and/or alternative systems for processing orders can find it difficult to keep WooCommerce order statuses up to date. Perhaps at a most basic level, the store owner receives a New Order email and simply picks and packs the items from the information in the email. Logging into the admin area to update the order status could seem like an unnecessary administrative step that provides no real benefit.
Taking the time to evaluate the WooCommerce Orders section, and update the Order statuses can have multiple benefits. It can prevent Orders from slipping through the cracks if an email goes astray. It can help utilise reports better, giving valuable insights into sales for future planning etc. It can be used as a great marketing opportunity, seizing a valid reason to email your customer and including extra information about your products and services. It can also be useful if selling physical tickets through WooCommerce, knowing if the customer has ‘used’ the ticket by means of updating the Order status (more on this use case later in the post).
Why modify a WooCommerce order via scanning a QR code?
If updating the order by logging into WooCommerce, navigating to the Orders screen, finding the relevant Order and updating the status (either per Order or by a bulk action) seems too laborious, then perhaps the answer lies in scanning a QR code with a mobile device. This could help combine admin tasks with real world Order fulfilment tasks. I.e. If the New Order email is being used as a packing slip, add a QR code to the email, then once the items have been dispatched scan it with a mobile device which updates the status. This way you get all the benefits from above, with much less leg work.
Another use case is physical entry tickets when not using a custom build ticketing solution. I.e. Someone purchases a ticket to an event or attraction on line, they turn up to the attraction with their Order Processing email in hand, how does the attraction owner know they won’t come back tomorrow and use the same ticket again? Order statuses! Likely when faced with a queue of visitors, it will not be convenient to pull up the Order and change status for each ticket. What if on their Order Processing email which they need to show you, they had a QR code embedded? Simply scan the code, programatically change the Order from Processing to Completed. If the order is already set to Completed, the ticket has already been scanned, the ticket is no longer valid. Just having this type of system in place will also prevent bad actors from multiple entries.
How to modify WooCommerce (without a plugin!) to update an order status by QR code
If these use cases sound familiar, or you have an instance that scanning a QR code to perform a WooCommerce related task could improve efficiencies, then get in touch with us, we would be happy to find a way to help.
If you are not afraid of jumping into your WordPress theme and making a few code changes, then the following code snippets are hopefully of some use to you.
- Create a WordPress Page to act as the ingress. (E.g. Scan Code)
- Create a file in your theme using the WordPress template override pattern. (E.g.
page-scan-code.php
) - Add the following code block to that page:
<?php
if( current_user_can( 'edit_posts' ) ):
$order = wc_get_order( $_GET['order_id'] );
if(!$order) {
exit('No such order');
} // If no $order
$status = $order->get_status();
if($status == 'completed') {
exit('Error: Order already Completed');
} elseif($status == 'processing') {
$order->update_status( 'completed' );
exit('Success: Order marked as Completed');
} // if $status
else:
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';
$full_url = $protocol . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
wp_redirect(wp_login_url($full_url));
endif; // If authorised
Depending on your exact requirements, you may choose to not force authentication to process this request, in which case you can remove the outer `if` condition.
Now we need to add the QR code to the relevant emails. You might choose to using the `woocommerce_email_order_details` hook. Google also provide a great little service (chart.googleapis.com) to generate QR codes.
The code could look like this, and should sit somewhere in your `functions.php` (or `include` files therein):
function namespace_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ){
$admin_url = home_url() . '/scan-code/?order_id=' . $order->get_id();
echo '<img src="https://chart.googleapis.com/chart?chs=400x400&cht=qr&chl='.$admin_url.'" width=400 height=400 alt="qr code" />';
}
add_action ('woocommerce_email_order_details', 'namespace_woocommerce_email_order_details', 100, 4);
Now a QR code will show under the Order details whenever they are emailed from the store, ready to be scanned whenever makes sense in the Order fulfilment cycle.
If you have any questions, please feel free to get in touch. We have extensive experience in Ecommerce, WooCommerce and other platforms.