I have Advanced Custom Fields setup for post type on WooCommerce Products. So each product has 1 unique custom field.
I’m trying to display the custom field after the product name on the cart and the checkout page and order table information.
However, running into problems as my code doesn’t display any output.
Any advice on how to achieve this would be much appreciated. Thanks
// Display the ACF custom field 'location' after the product title on cart / checkout page. function cart_item_custom_feild( $cart_item ) { $address = get_field( 'location', $cart_item['product_id'] ); echo "<div>Address: $address.</div>"; } add_action( 'woocommerce_after_cart_item_name', 'cart_item_custom_feild', 10, 1 );
I also tried the_field
instead of get_field
Advertisement
Answer
1- On the cart page AND on the order review table on the checkout page
If you would need to run it both on the cart page AND order review table on the checkout page, you could use woocommerce_cart_item_name
filter hook, like this:
add_filter('woocommerce_cart_item_name', 'order_review_custom_field', 999, 3); function order_review_custom_field($product_name, $cart_item, $cart_item_key) { $address = get_field('location', $cart_item['product_id']); return ($address) ? $product_name . '<div>Address: ' . $address . '</div>' : $product_name . '<div>Address: No address found!</div>'; }
Here’s the result on the cart page:
And on the order review table on the check out page:
2- On the email AND in the order details table on the thank you page:
We could use woocommerce_order_item_meta_end
action hook to append the custom field value to the end of product name on the email template:
add_action("woocommerce_order_item_meta_end", "email_order_custom_field", 999, 4); function email_order_custom_field($item_id, $item, $order, $plain_text) { $address = get_field('location', $item->get_product_id()); echo ($address) ? '<div>Address: ' . $address . '</div>' : '<div>Address: No address found!</div>'; };
And here’s the result on the email:
And in the order details table on the thank you page:
This answer has been fully tested on woocommerce 5.7.1
and works.