1

Is there a way to display my custom SKU under each product on the WooCommerce order page?

The custom sku displays fine when I edit the product, but it does not display in the order page for the product. I need this information to show on the order so that Zapier can match it with the Visma Account Software ArticleID of the product.

This attempt is based on the solution How to add a (second) custom sku field to WooCommerce products?

// Add Custom SKU Field

function my_add_custom_sku() {
    $args = array(
        'label' => __( 'ArticleID', 'woocommerce' ),
        'placeholder' => __( 'Enter Visma ArticleID Here', 'woocommerce' ),
        'id' => 'articleid',
        'desc_tip' => true,
        'description' => __( 'Visma ArticleID is for integration with Zapier.', 'woocommerce' ),
    );
    woocommerce_wp_text_input( $args );

}
add_action( 'woocommerce_product_options_sku', 'my_add_custom_sku' );

// Save
function my_save_custom_meta( $product ){
    if( isset($_POST['articleid']) ) {
        $product->update_meta_data( 'articleid', sanitize_text_field( $_POST['articleid'] ) );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'my_save_custom_meta', 10, 1 );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

1

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 key slug with a readable label name "ArticleID" (on customer orders and email notifications):

// Replace the label (slug) by a readable label name on orders and emails
add_filter( 'woocommerce_order_item_display_meta_key', 'filter_order_item_display_meta_key', 10, 3 );
function filter_order_item_display_meta_key( $display_key, $meta, $item ) {
    // Not on Admin
    if ( ! is_admin() && $display_key === 'articleid' ) {
        $display_key = __('ArticleID', 'woocommerce');
    }
    return $display_key;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Related to this thread:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks. I'm looking at your solution here (https://stackoverflow.com/questions/52569890/pass-custom-product-meta-data-to-the-order-in-woocommerce-3) and this is what I am trying to achieve. All I want is for my custom SKU (articleid) to appear as a hidden order item meta data, visible only in Admin Order edit pages on each item. – Wanderlust Consulting Jan 24 '21 at 22:28
  • This works. Thank you. But it displays on the emails too. How do I amend it to only be visible on the Admin Order edit page? – Wanderlust Consulting Jan 24 '21 at 22:56