How to retrieve cart_item_data with WooCommerce?
Developers often assume that adding custom data to a cart item via the woocommerce_add_cart_item_data filter is sufficient for persistence across the session. However, a common stumbling block occurs when attempting to retrieve this data later using $cart->get_item_data(). While the data persists in the serialized string within the session (accessible via debugging tools), it often appears as an empty array or null when accessed through standard API calls.
This behavior stems from how WooCommerce manages cart items internally versus how it exposes them to developers. When you use WC()->cart->get_cart(), WooCommerce iterates over the cart contents and reconstructs each item into a clean associative array. By design, this reconstruction process excludes custom data attached via the woocommerce_add_cart_item_data filter unless that data is explicitly included in the item's metadata structure. The raw serialized data found deep within $WC()->session->_data->cart contains your fields, but iterating over the cart surface does not automatically unpack them into the item object you receive in the loop.
To successfully retrieve this custom data, you must bypass the standard iteration and access the underlying session storage directly or utilize a specific method that WooCommerce provides for fetching item-specific data. The WC_Cart class offers a dedicated method for this exact purpose: get_item_data(). However, there is a critical nuance regarding how you invoke it.
If you pass the entire $cart_item object returned by get_cart() into get_item_data(), WooCommerce attempts to read from the item's internal properties. Since the custom data stored via the filter was never merged into those properties during the reconstruction phase, the method returns an empty result. The solution is to pass the unique cart key (the session ID for that specific item) instead of the array object itself.
Here is how you should structure your retrieval logic:
// Iterate through the cart to get the keys
foreach ( WC()->cart->get_cart() as $key => $cart_item ) {
// Retrieve custom data using the key, not the item object
$custom_data = WC()->cart->get_item_data( $key );
if ( ! empty( $custom_data['class-date'] ) ) {
// Now you can access your stored field safely
echo 'Class Date: ' . esc_html( $custom_data['class-date'] );
}
}
By passing the $key to get_item_data(), you force WooCommerce to look up the original entry in the session data where the serialized custom fields reside and deserialize them on demand. This approach ensures that your custom plugin data remains accessible regardless of how the cart item array is structured internally.
It is also worth noting that if you need to persist this data beyond the current session (e.g., via cookies or database), you must ensure the data is sanitized before storage, as $_REQUEST values are not inherently trusted. The filter hook allows you to manipulate $cart_item_data before it is serialized, but retrieval requires respecting the session-backed architecture of WooCommerce's cart engine.
For teams working with complex e-commerce architectures, understanding the distinction between the "view" layer (the clean arrays returned by get_cart()) and the "storage" layer (serialized session data) is essential. If you are building a plugin that relies on these custom fields, ensure your retrieval logic always targets the unique cart key to guarantee consistency across different WooCommerce versions and configurations.