Reading list Switch to dark mode

    How to Make WooCommerce Plugin High Performance Order Storage Compatible?

    Updated 1 November 2023

    WooCommerce plugin HPOS (High-performance order storage) involves optimizing how orders are stored and managed within the WooCommerce database to ensure efficient and speedy operations.

    We are implementing High-performance order storage compatibility on our popular plugins like WooCommerce Marketplace and WooCommerce POS.

    WooCommerce is a popular e-commerce platform for WordPress, and as the store grows, it’s important to maintain the performance of order processing and storage.

    We’ll delve into the strategies and best practices for achieving high-performance order storage in WooCommerce.

    Optimize Your Database

    WooCommerce relies heavily on your WordPress database for storing order data. To ensure high performance, it’s essential to keep your database in top shape.

    Searching for an experienced
    Woocommerce Company ?
    Find out More

    Regularly perform tasks like cleaning up unnecessary data, removing unused tables, and optimizing database queries.

    You can use WooCommerce plugins specifically designed for database optimization or seek the assistance of a developer experienced with WooCommerce.

    Utilize Efficient Hosting

    Your choice of hosting provider has a profound impact on your store’s performance. Ensure your hosting plan offers sufficient server resources to handle your order volume and web traffic.

    If possible, opt for a hosting solution optimized for WooCommerce. The right hosting environment can make a world of difference.

    Explore a wide range of WooCommerce hosting services to set up, migrate, and optimize your online store on AWS (Amazon Web Services) and other cloud platforms.

    Effective Indexing

    Properly indexing the database tables related to orders can drastically improve the speed of database queries.

    Indexing essentially creates a roadmap for the database to locate information quickly.

    If you’re not comfortable with database management, it’s best to consult with a developer to set up effective indexing for your WooCommerce orders.

    Limit Custom Fields

    While custom fields in WooCommerce orders can be useful for capturing specific data, they can also slow down order storage and retrieval.

    To maintain high performance, only use custom fields when absolutely necessary.

    Steps to make compatibility with WooCommerce plugin HPOS:

    Declare the compatibility with WooCommerce plugin HPOS-

    Added the following code in your plugin main file to declare compatibility-

    add_action('before_woocommerce_init', function(){
    
        if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
    
        }
    
    });

    Functions for getting/setting order & order meta

    Any code getting orders from the get_posts directly can be converted to a wc_get_order call instead:

    // Instead of
    $order = get_post( $order_id ); // returns WP_Post object.
    // use
    $order = wc_get_order( $order_id ); // returns WC_Order object.

    For interacting with metadata, use the update_meta_data, add_meta_data and delete_meta_data methods on the order object, followed by a save call.

    WooCommerce will take care of figuring out which tables are active and saving data in appropriate locations.

    // Instead of following update/add/delete methods, use:
    update_post_meta( $post_id, $meta_key_1, $meta_value_1 );
    add_post_meta( $post_id, $meta_key_2, $meta_value_2 );
    delete_post_meta( $post_id, $meta_key_3, $meta_value_3 );
    get_post_meta( $post_id, $meta_key_4, true);
    // use
    $order = wc_get_order( $post_id );
    $order->update_meta_data( $meta_key_1, $meta_value_1 );
    $order->add_meta_data( $meta_key_2, $meta_value_2 );
    $order->delete_meta_data( $meta_key_3, $meta_value_3 );
    $order->get_meta( $meta_key_4, true );
    $order->save();

    Calling the save() method is a relatively expensive operation, so you may wish to avoid calling it more times than necessary.

    (For example, if you know it will be called later in the same flow, you may wish to avoid additional earlier calls when operating on the same object).

    Check if the post is a WooCommerce order

    You can check if the post is a WooCommerce order by the following code –

    if ( ! function_exists( 'wkwc_is_wc_order' ) ) {
    	/**
    	 * Check is post WooCommerce order.
    	 *
    	 * @param int $post_id Post id.
    	 *
    	 * @return bool $bool True|false.
    	 */
    	function wkwc_is_wc_order( $post_id = 0 ) {
    		$bool = false;
    		if ( 'shop_order' === OrderUtil::get_order_type( $post_id ) ) {
    			$bool = true;
    		}
    
    		return $bool;
    	}
    }

    Conclusion

    How to make WooCommerce plugin High-performance order storage compatible

    That’s all about the implementation of the WooCommerce plugin HPOS ( High-performance order storage ) feature in your WooCommerce plugins.

    . . .

    Leave a Comment

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


    2 comments

  • Alice
    • Ajeet Kumar (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home