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


哎?小查查
2025-03-13 10:29:13 (9天前)


我有这个代码在产品页面上创建一个下拉列表。它保存所选数据并显示在订单上。我需要帮助的是在自定义列中显示值。

这是……

2 条回复
  1. 0# نسر الصحراء | 2019-08-31 10-32



    要在自定义列中显示此自定义订单商品元数据,请使用以下内容:




    1. // 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;

    2. if( $column  == 'cost_center' ) {
    3.     $values = []; // Initializing
    4.     // Loop through order items
    5.     foreach ( $the_order->get_items() as $item ) {
    6.         if( $cost_centre = $item->get_meta( 'Cost Center' ) ) {
    7.             $values[] = $cost_centre;
    8.         }
    9.     }
    10.     // Display the value(s)
    11.     if( sizeof( $values ) > 0 ) {
    12.         echo implode( ', ', $values); // Convert the array to a coma separated string
    13.     }
    14. }
    15. }

    16. </code>


    代码在您的活动子主题(或活动主题)的function.php文件中。它应该有效。


登录 后才能参与评论