You could use the hook woocommerce_checkout_create_order_line_item
to save this product custom field as custom order Item data, when order is placed, as follows (and display it everywhere on orders and emails):
// Save as custom order item meta data and display on orders and email notifications
add_action( 'woocommerce_checkout_create_order_line_item', 'add_articleid_on_orders_and_emails', 10, 4 );
function add_articleid_on_orders_and_emails( $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 ( empty($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 ( ! empty($articleid) ) {
$item->add_meta_data( 'articleid', $articleid ); // add it as custom order item meta data
}
}
And the following to change "articleid" displayed label slug with "ArticleID" readable label name (on customer orders and email notifications):
// Replace the label (slug) by a readable label name on orders and emails
add_filter( 'woocommerce_display_item_meta', 'filter_order_item_articleid_displayed_label', 100, 3 );
function filter_order_item_articleid_displayed_label( $html, $item, $args ) {
// Not on admin
if ( ! is_admin() && $item->get_meta('articleid') ) {
$html = str_replace('articleid', __('ArticleID', 'woocommerce'), $html);
}
return $html;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related to this thread:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…