Add custom product taxonomy to woocommerce orders table
I'm needing help trying to add a custom product taxonomy to the orders table of Woocommerce. I have created a php function that works elsewhere on the site that pulls it into the cart table just fine, however it doesn't pull anything into the orders table. If you could take a look at my code and see what I'm missing that would be great! I have a feeling it has to do with calling the product_id instead of the item_id but I tried switching out the two and nothing happened.
add_action( 'woocommerce_order_item_meta_start', 'declaration_order_email_pages', 9999, 4 );
function declaration_order_email_pages( $item_id, $item, $order, $plain_text ) {
$terms = get_the_terms( $product_id, 'declarations' );
$product_cat = array();
foreach ($terms as $term) {
$product_cat[] .= $term->name;
}
echo implode(', ', $product_cat);}
The problem is that there is no $product_id in your code. but since in this hook you get $item object you can get product id like this $item->get_product_id():
add_action('woocommerce_order_item_meta_start','declaration_order_email_pages',9999,4 );
function declaration_order_email_pages( $item_id, $item, $order, $plain_text ) {
$terms = get_the_terms( $item->get_product_id(), 'declarations' );
$product_cat = array();
foreach ($terms as $term) {
$product_cat[] .= $term->name;
}
echo implode(', ', $product_cat);
}