Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

php - Display a product custom field only in WooCommerce Admin single orders

This question follows How to show a product custom field (custom SKU) in WooCommerce orders answer to my previous question.

How do I make a product custom field articleid (custom SKU) to be visible only in Admin Order edit pages for each order item?

Also, it does not work for manual orders. How to display a product custom field articleid (custom SKU) on manual orders too?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Updated last function to avoid errors with other order item types that "line items".

To make it only visible on admin, In your last function, you will need to change the order item meta key from 'articleid' to '_articleid' (adding an underscore at the beginning of the key) like:

// 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
    }
}

For manual orders you will use the following:

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

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

Related to this thread:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...