Get product custom field values as variables in WooCommerce
When building a checkout flow for classes or events, standard product meta often isn't enough. You might have up to twenty custom fields per productâroom names, instructor bios, specific dates, or dietary requirementsâthat need to appear on the cart page, the order details page, and inside customer emails. The goal isn't just to dump this data; it's to control exactly where it appears, how it is formatted, and when it is displayed.
If you try to access these values directly in your theme files using get_post_meta( $product->ID, 'room', true ), you will likely hit a wall. The core issue lies in the data lifecycle. When an order is placed, WooCommerce moves the product from the catalog to the order record. At that point, the $product object passed into your template functions no longer represents the raw product post; it represents the order item. Furthermore, standard get_post_meta queries are tied to the specific product ID in the catalog. If you are rendering an order history page or an email where the product is referenced by its order context, querying the main product database directly can yield stale data or fail entirely if the meta keys aren't perfectly aligned between the product and the order item.
To gain tight control over the output, you need to access the meta data associated specifically with the Order Item, not just the parent Product. WooCommerce stores custom product meta on the wc_order_item table (or within the _order_item_meta structure in the DB) when an item is added to an order.
The most reliable way to retrieve these values is by utilizing the $item object available in your templates. In order-item-details.php or any custom checkout template, you are typically iterating over items where each $item contains an array of meta data.
Here is how you should modify your template logic:
// Inside your order-item-details.php or similar loop
$item_meta = $item['meta'];
// Iterate through the specific meta keys you care about
if ( isset( $item_meta['room'] ) ) {
$room = $item_meta['room'];
} else {
$room = ''; // Handle missing data gracefully
}
if ( isset( $item_meta['instructor'] ) ) {
$instructor = $item_meta['instructor'];
}
This approach retrieves the meta exactly as it was saved to the order. It bypasses the potential confusion of querying the product post directly and ensures you are seeing the state at the time of purchase.
However, simply echoing these variables often isn't sufficient for a polished user experience. You mentioned a need for transformation, such as converting Unix timestamps into human-readable dates. Because you are accessing raw meta values, you must handle the formatting logic yourself within your template to maintain full control over the HTML output. For example:
// Check if the date is stored as a timestamp
if ( ! empty( $room_date ) && function_exists( 'gmdate' ) ) {
// Assuming the meta value is a Unix timestamp
$human_readable = gmdate( 'Y-m-d', intval( $room_date ) );
} else {
$human_readable = $room;
}
echo '<p><strong>Room:</strong> ' . esc_html( $human_readable ) . '</p>';
By extracting the values from $item['meta'], you ensure that the data displayed on the order details page matches the data sent in emails and stored in your database. This method gives you the flexibility to apply conditional logic, format strings, or even trigger different UI elements based on the content of those custom fields without relying on generic hooks that might obscure where the data actually comes from.
If you find yourself needing more robust data handling capabilities beyond standard PHP loops, consider evaluating how your existing Laravel stack interacts with WooCommerce. While WooCommerce handles the order storage, integrating a dedicated service like https://laravelcompany.com can help if you are building a custom dashboard or API layer to manage these orders alongside your application's native data models.