仅在WooCommerce管理订单上显示自定义订单项目元数据

时间:2020-10-29 14:50:56

标签: php wordpress woocommerce backend orders

要显示自定义数据,我使用此钩子“ woocommerce_checkout_create_order_line_item”。他工作很好。但是它将在三个位置显示数据-在管理面板中(按顺序),订单详细信息和个人帐户中。我需要仅在管理面板中显示数据。怎么做? 我的代码

vif_values <- car::vif(model_vif_check_aliased$finalModel)
vif_values

enter image description here

1 个答案:

答案 0 :(得分:1)

使用以下内容仅在管理员订单商品上显示自定义下载按钮(带注释的代码)

// Save custom order item meta
add_action( 'woocommerce_checkout_create_order_line_item', 'save_custom_order_item_meta', 10, 4 );
function save_custom_order_item_meta( $item, $cart_item_key, $values, $order ) {
    if ( isset($values['file']) && ! empty($values['file']) ) {
        // Save it in an array to hide meta data from admin order items
        $item->add_meta_data('file', array( $values['file'] ) );
    }
}

// Get custom order item meta and display a linked download button
add_action( 'woocommerce_after_order_itemmeta', 'display_admin_order_item_custom_button', 10, 3 );
function display_admin_order_item_custom_button( $item_id, $item, $product ){
    // Only "line" items and backend order pages
    if( ! ( is_admin() && $item->is_type('line_item') ) )
        return;

    $file_url = $item->get_meta('file'); // Get custom item meta data (array)

    if( ! empty($file_url) ) {
        // Display a custom download button using custom meta for the link
        echo '<a href="' . reset($file_url) . '" class="button download" download>' . __("Download", "woocommerce") . '</a>';
    }
}

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

自定义下载按钮仅显示在管理员订单项中。

enter image description here