在 WooCommerce 管理订单项上显示产品自定义字段,也适用于可变产品

问题描述

基于 Show custom fields on the order editing page in WooCommerce 答案代码,我做了一些细微的更改:

add_action( 'woocommerce_before_order_itemMeta','add_admin_order_item_custom_fields',10,2 );
function add_admin_order_item_custom_fields( $item_id,$item ) {
    // targeting line items type only
    if( $item->get_type() !== 'line_item' ) return;

    $product = $item-> get_product();
    $value1  = $product->get_Meta('model_number');

    if ( ! empty($value1) ) {
        echo '<table cellspacing="0" class="display_Meta">';

        if ( ! empty($value1) ) {
            echo '<tr><th>' . __("Modelnummer","woocommerce") . ':</th><td>' . $value1 . '</td></tr>';
        }
        echo '</table>';
    }
}

我想让该代码也适用于可变产品自定义字段

我需要更改什么才能使其适用于可变产品

解决方法

如果是没有为其设置自定义字段的产品变体,以下将获取父变量产品自定义字段:

AVAudioFile

代码位于活动子主题(或活动主题)的functions.php 文件中。经测试有效。