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' ) ?
wc_get_container()
->get( CustomOrdersTableController::class )
->custom_orders_table_usage_is_enabled()
: false;
}
PHPRegister 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';
add_meta_box(
'META_BOX_ID',
__( 'META BOX TITLE', 'TEXT_DOMAIN' ),
'display_content_html',
$screen,
'side',
'default'
);
}
PHPOrder 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 );
PHPBut 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
$order->save();
PHPSaving 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 );
PHPOthers
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
PHPAlso WP query_posts() will not be working. Instead, you need to use wc_get_orders, You can find complete guide from here.
Thanks.
Hi there, just became alert to your blog through Google,
and found that it’s truly informative. I am gonna watch out for brussels.
I will appreciate if you continue this in future.
Many people will be benefited from your writing. Cheers! Escape room lista