Created
December 4, 2020 10:42
-
-
Save madeincosmos/d89bd44b01aea4b1374110647d033db6 to your computer and use it in GitHub Desktop.
Save categories in order line item metadata
This file contains hidden or 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
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