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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!