Ecommerce Shopify WordPress Discussion

Get ACF WooCommerce product value from order Item

I'm using Woocommerce and when the payment is being made, this function is executed and it works correctly, but what I need is for the value of the custom field to be returned when executing the foreach. What function returns the content of the field, I tried the_field('comboitems') but without success. function example_metadata( $metadata, $order, $post ) { foreach ( $order->get_items() as $item_id => $item ) { $metadata['test'] = '1234'; $metadata['field1'] = $item->get_data(); $metadata['customfield'] = the_field('comboitems'); } return $metadata; } This $item->get_data() was an attempt to check if the comboitems custom field was read. I used the command $item->get_data(); in an attempt to see if it returned something related to my field.. I tried using the_field('comboitems') but it returned NULL, as well as other variations in search of a solution.
Don't use the_field() function as it echoes a custom field. Use instead get_field() to assign a custom field value to a variable… Then from an order item (product), you will use: $metadata['customfield'] = get_field('comboitems', $item->get_product_id()); So in your code: function example_metadata( $metadata, $order, $post ) { foreach ( $order->get_items() as $item ) { $metadata['customfield'] = get_field('comboitems', $item->get_product_id()); } return $metadata; }

January 7, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.