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

时间:2021-03-24 12:02:56

标签: php wordpress woocommerce custom-fields orders

基于 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>';
    }
}

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

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

1 个答案:

答案 0 :(得分:1)

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

AVAudioFile

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