Skip to content
Advertisement

How to show a product custom field (custom SKU) in WooCommerce orders

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 );

Advertisement

Answer

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:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement