仅在WooCommerce Admin单笔订单中显示产品自定义字段 [英] Display a product custom field only in WooCommerce Admin single orders

查看:42
本文介绍了仅在WooCommerce Admin单笔订单中显示产品自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题遵循

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

如何使产品自定义字段 articleid (自定义SKU)仅在每个订单商品的管理订单"编辑页面中可见?

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

此外,它不适用于手动订单.如何在手动订单上也显示商品自定义字段 articleid (自定义SKU)?

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

推荐答案

更新了最后一个功能,以避免其他与订单项"相对的订单项出现错误.

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

要使其仅在管理员上可见,在最后一个功能中,您需要将订单项元键从'articleid'更改为'_ articleid'(添加键开头的下划线),如:

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

代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

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

与此线程相关:

这篇关于仅在WooCommerce Admin单笔订单中显示产品自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆