Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Last active April 16, 2023 18:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugin-republic/54d9f69b6a42e821d5dd4a7892fc4fc5 to your computer and use it in GitHub Desktop.
Save plugin-republic/54d9f69b6a42e821d5dd4a7892fc4fc5 to your computer and use it in GitHub Desktop.
<?php
/**
* Get add-on metadata from each line item in the order
* @param $order_id
* @param $metakey The add-ons metakey (field label), usually prefixed by an underscore
*/
function prefix_get_addons_metadata_by_key( $order_id, $metakey=false ) {
$order = wc_get_order( $order_id );
$order_line_items = $order->get_items();
foreach( $order_line_items as $line_item ) {
// Here, we can iterate through all the meta for this line item
$all_meta = $line_item->get_meta_data();
if( $all_meta ) {
foreach( $all_meta as $meta ) {
$meta_id = $meta->id;
$meta_key = $meta->key;
$meta_value = $meta->value;
}
}
// Here, we can get the value by a specific metakey
if( $metakey ) {
$meta_value = $line_item->get_meta( $metakey );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment