Last active
April 16, 2023 18:46
-
-
Save plugin-republic/54d9f69b6a42e821d5dd4a7892fc4fc5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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