WooCommerce: HPOS for developers

Recently, WooCommerce introduced HPOS (High-performance order storage) and it’s now default as Order storage. It’s breaking changes feature cause some issues about plugins compatibility.

Meta box

At first you need to check HPOS is enabled or not! If it’s enabled, you can get page screen id by wc_get_page_screen_id function.

Let’s define a function to check about HPOS enabled

use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;

 * Check if HPOS enabled.
function is_wc_order_hpos_enabled() {
	  return function_exists( 'wc_get_container' ) ? 
	            ->get( CustomOrdersTableController::class )
	        : false;

Register meta boxes,

add_action( 'add_meta_boxes', 'register_meta_boxes' );

 * Register metaboxes under order details page.
public function register_meta_boxes() {
	$screen = is_wc_order_hpos_enabled()
		        ? wc_get_page_screen_id( 'shop-order' )
		        : 'shop_order';


Order Meta

According to WooCommerce HPOS upgrade guide, You need to do few changes in your code for create, delete, update order meta data.

Previously, some developers uses WP’s postmeta functions to save meta data.

// Get order meta data
get_post_meta( $order_id, META_KEY, SINGLE?);
// Create order meta
add_post_meta( $order_id, META_KEY, META_VALUE );
// Create or update order meta
update_post_meta( $order_id, META_KEY, META_VALUE );
// Delete order meta
delete_post_meta( $order_id, META_KEY, META_VALUE );

But these functions isn’t works anymore inside orders when HPOS is enabled.

So, You need to use WooCommerce order object’s get, add, update, delete methods.

$order = wc_get_order( $order_id );
// Get order meta data
$order->get_meta( META_KEY, SINGLE? );
// Create order meta
$order->add_meta_data( META_KEY, META_VALUE );
// Create or Update order meta
$order->update_meta_data( META_KEY, META_VALUE );
// Delete order meta
$order->add_meta_data( META_KEY, META_VALUE );

// save them after any changes

Saving Order

If you implement custom fields inside woocommerce admin order details page then you’ll need to save data when form is submitted.

Previously, we can achieve this by save_post_{post_type} hook. But now, You need to use woocommerce_process_shop_order_meta hook with save_post_{post_type} also.

add_action( 'save_post_shop_order', 'save_custom_order_data' );
add_action( 'woocommerce_process_shop_order_meta', 'save_custom_order_data', 60 );


Before HPOS, Every WooCommerce orders are posts under shop_order post_type and you can use some post related functions inside order details page.

global $post; // it's always null on HPOS
get_the_ID(); // return false on HPOS

Also WP query_posts() will not be working. Instead, you need to use wc_get_orders, You can find complete guide from here.


Istiaq Nirab

A highly skilled and proficient WordPress developer with over Three years of professional experience in all aspects of WordPress website creation, including design, plug-ins, and implementation. Results-driven individual contributor with a successful track record in exceeding expectations for web development initiatives

More Reading

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *