skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    LoicTheAztec, how can I make when don't is custom field but an attribute?


  2. 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;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search