Mastering WooCommerce: How to Add a Custom Column to Shop Order Table After HPOS Release

Mastering WooCommerce: How to Add a Custom Column to Shop Order Table After HPOS Release

Efficient order management is crucial for any WooCommerce store, especially with the introduction of High-Performance Order Storage (HPOS). This new feature optimizes how order data is stored and retrieved, providing a significant boost for stores with high transaction volumes. In this guide, we'll cover how to add a 'Used Coupons' column to your WooCommerce Shop Order Table, ensuring you have quick and easy access to this information even after the HPOS update.

Step 1: Understanding High-Performance Order Storage (HPOS)

High-Performance Order Storage (HPOS) is a game-changer for WooCommerce, moving away from custom post types (CPTs) and implementing custom database tables for order data. This shift results in faster queries and overall improved performance, especially noticeable in stores with a substantial number of orders.

Step 2: Check for HPOS Integration

Before proceeding, you need to check if your website is taking advantage of HPOS. Add the following code to your theme’s functions.php file or a custom plugin:

function scew_hpos_checker() {
    if ( class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) ) {
        if ( Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) {
            update_option( 'is_hpos_enabled', true );
        } else {
            update_option( 'is_hpos_enabled', false );
        }
    }
}
add_action( 'plugins_loaded', 'scew_hpos_checker' );

This function checks for HPOS and updates an option in the WordPress database accordingly.

Step 3: Add the 'Used Coupons' Column

Next, let’s add a new column to display used coupons in the WooCommerce orders list. Here are the complete callback functions:

/**
 * Adds a new 'Used Coupon' column to the orders list table.
 *
 * @param array $columns Existing columns.
 * @return array Updated list of columns.
 */
function custom_order_column( $columns ) {
    $new_columns = array();

    foreach ( $columns as $key => $value ) {
        if ( 'order_status' === $key ) {
            $new_columns['used_coupon'] = 'Used Coupon';
        }
        $new_columns[ $key ] = $value;
    }

    return $new_columns;
}

/**
 * Populates the 'Used Coupon' column in the orders list.
 *
 * @param string $column Current column.
 * @return void
 */
function custom_order_column_content( $column ) {
    global $post;

    if ( 'used_coupon' === $column ) {
        $order   = wc_get_order( $post->ID );
        $coupons = $order->get_coupon_codes();
        echo esc_html( strtoupper( implode( ', ', $coupons ) ) );
    }
}

/**
 * Populates the 'Used Coupon' column in the orders list for HPOS.
 *
 * @param string $column Current column.
 * @param WC_Order $order Current order.
 * @return void
 */
function hpos_custom_order_column_content( $column, $order ) {
    if ( 'used_coupon' === $column ) {
        $coupons = $order->get_coupon_codes();
        echo esc_html( strtoupper( implode( ', ', $coupons ) ) );
    }
}

Ensure that these functions are added to your theme’s functions.php file or a custom plugin.

Step 4: Implement the Hooks

Finally, add the following code to ensure the functions from Step 3 are hooked correctly, depending on whether HPOS is enabled:

if ( ! get_option( 'is_hpos_enabled' ) ) {
    add_filter( 'manage_edit-shop_order_columns', 'custom_order_column' );
    add_action( 'manage_shop_order_posts_custom_column', 'custom_order_column_content' );
} else {
    add_filter( 'woocommerce_shop_order_list_table_columns', 'custom_order_column' );
    add_action( 'woocommerce_shop_order_list_table_custom_column', 'hpos_custom_order_column_content', 10, 2 );
}

Conclusion

By following this guide, you've successfully added a 'Used Coupons' column to your WooCommerce Shop Order Table. This addition will streamline your order management process, giving you quick access to coupon data, regardless of whether your store is utilizing HPOS. Happy selling and enjoy your enhanced WooCommerce experience!