Php 仅在单个订单中显示产品自定义字段

Php 仅在单个订单中显示产品自定义字段,php,wordpress,woocommerce,hook-woocommerce,orders,Php,Wordpress,Woocommerce,Hook Woocommerce,Orders,这个问题是在回答我上一个问题之后提出的 如何使产品自定义字段articleid(自定义SKU)仅在每个订单项目的管理订单编辑页面中可见 此外,它不适用于手动订单。如何在手动订单上也显示产品自定义字段articleid(自定义SKU)?更新了上次功能,以避免其他订单项目类型出现“行项目”错误 要使其仅在管理员上可见,在上一个函数中,您需要将订单项元键从'articleid'更改为''u articleid'(在键的开头添加下划线),如下所示: 对于手动订单,您将使用以下命令: add_action

这个问题是在回答我上一个问题之后提出的

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


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

更新了上次功能,以避免其他订单项目类型出现“行项目”错误

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

对于手动订单,您将使用以下命令:

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文件。测试和工作

与此线程相关:


非常感谢您!了解这一点非常有用。是否有办法更改管理订单页面中的标签?我注意到它现在显示为“_articleid”。@WanderlustConsulting是的,如果有可能,问一个新问题并通知我……我会回答的。刚刚发布了问题。非常感谢你的帮助。
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 );
        }
    }
}