Skip to content

Instantly share code, notes, and snippets.

@madeincosmos
Created December 4, 2020 10:42
Show Gist options
  • Save madeincosmos/d89bd44b01aea4b1374110647d033db6 to your computer and use it in GitHub Desktop.
Save madeincosmos/d89bd44b01aea4b1374110647d033db6 to your computer and use it in GitHub Desktop.
Save categories in order line item metadata
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_save_category_data_on_the_order', 20, 4 );
function custom_checkout_save_category_data_on_the_order( $item, $cart_item_key, $values, $order ) {
if ( $item->is_type( 'line_item' ) ) {
// Get the product categories for this item
$terms = wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'names' ) );
// Save a coma separated string of product category names in line item meta
$item->update_meta_data( 'Categories', implode( ', ', $terms ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment