PHP Snippet 1:
// Save as custom order item meta data and display on admin single orders
add_action( 'woocommerce_checkout_create_order_line_item', 'add_articleid_as_orders_item_meta', 10, 4 );
function add_articleid_as_orders_item_meta( $item, $cart_item_key, $values, $order ) {
$articleid = $values['data']->get_meta('articleid'); // Get product "articleid"
// For product variations when the "articleid" is not defined
if ( ! $articleid && $values['variation_id'] > 0 ) {
$product = wc_get_product( $values['product_id'] ); // Get the parent variable product
$articleid = $product->get_meta( 'articleid' ); // Get parent product "articleid"
}
if ( $articleid ) {
$item->add_meta_data( '_articleid', $articleid ); // add it as custom order item meta data
}
}
PHP Snippet 2:
add_action( 'woocommerce_before_save_order_item', 'action_before_save_order_item_callback' );
function action_before_save_order_item_callback( $item ) {
// Targeting only order item type "line_item"
if ( $item->get_type() !== 'line_item' )
return; // exit
$articleid = $item->get_meta('articleid');
if ( ! $articleid ) {
$product = $item->get_product(); // Get the WC_Product Object
// Get custom meta data from the product
$articleid = $product->get_meta('articleid');
// For product variations when the "articleid" is not defined
if ( ! $articleid && $item->get_variation_id() > 0 ) {
$product = wc_get_product( $item->get_product_id() ); // Get the parent variable product
$articleid = $product->get_meta( 'articleid' ); // Get parent product "articleid"
}
// Save it as custom order item (if defined for the product)
if ( $articleid ) {
$item->update_meta_data( '_articleid', $articleid );
}
}
}