dtqu72830 2019-02-27 13:59
浏览 231
已采纳

在订单视图列WooCommerce中显示自定义字段值

I have this code which creates a drop-down on the product page. It saves the data chosen and it displays on the order. What I am need help with is displaying the value in the custom column.

Here's the code:

add_action( 'woocommerce_product_options_general_product_data', 'costcenter_management_group' );
function costcenter_management_group() {
global $post;
echo '<div class="options_group">';
woocommerce_wp_select( array(
'id'      => '_cost_center',
'label'   => __( 'Cost Center', 'woocommerce' ),
'options' => array(
'One' => __( 'Office One', 'woocommerce' ),
'Two' => __( 'Office Two', 'woocommerce' ),
)));
echo '</div>';
}

// save cost center settings
add_action( 'woocommerce_process_product_meta', 'costcenter_management_group_save' );
function costcenter_management_group_save( $post_id ){
if( isset( $_POST['_cost_center'] ) )
update_post_meta( $post_id, '_cost_center', esc_attr( $_POST['_cost_center'] ) );
}

// display on order
add_action('woocommerce_checkout_create_order_line_item', 'costcenter_management_group_display', 20, 4);
function costcenter_management_group_display($item, $cart_item_key, $values, $order) {
if ( $cost_center = $values['data']->get_meta('_cost_center') ) {
$item->update_meta_data( 'Cost Center', $cost_centre );
}}

// add cost center column on order view (WC admin)
add_filter('manage_edit-shop_order_columns', 'costcenter_management_group_column', 10, 1 );
function costcenter_management_group_column( $columns ) {
$actions_key = isset($columns['wc_actions']) ? 'wc_actions' : 'order_actions';
$order_actions = $columns[$actions_key];
unset($columns[$actions_key]);
$columns['cost_center'] = __("Cost Center", "woocommerce");
$columns[$actions_key] = $order_actions;
return $columns;
}

I know I can use echo but I don't know which value I'm supposed to use?

  • 写回答

1条回答 默认 最新

  • dongleibeng5602 2019-02-27 14:23
    关注

    To display this custom order item metadata in your custom column use the following:

    // Display data to custom column in admin orders list
    add_action( 'manage_shop_order_posts_custom_column' , 'display_enclosed_invoice_order_column_data' );
    function display_enclosed_invoice_order_column_data( $column ) {
        global $the_order, $post;
    
        if( $column  == 'cost_center' ) {
            $values = []; // Initializing
    
            // Loop through order items
            foreach ( $the_order->get_items() as $item ) {
                if( $cost_centre = $item->get_meta( 'Cost Center' ) ) {
                    $values[] = $cost_centre;
                }
            }
            // Display the value(s)
            if( sizeof( $values ) > 0 ) {
                echo implode( ', ', $values); // Convert the array to a coma separated string
            }
        }
    }
    

    Code goes on function.php file of your active child theme (or active theme). It should works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决
  • ¥15 Python语言实验
  • ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
  • ¥20 电脑重启停留在grub界面,引导出错需修复
  • ¥15 matlab透明图叠加
  • ¥50 基于stm32l4系列 使用blunrg-ms的ble gatt 创建 hid 服务失败
  • ¥150 计算DC/DC变换器平均模型中的参数mu