在WooCommerce订单页面的“自定义”列上显示自定义字段“ Handleby”?

时间:2018-09-20 17:10:40

标签: php wordpress woocommerce custom-fields

我试图通过管理员订单页面上的自定义列显示一个自定义字段,该字段添加到从网站前端(称为“ Handleby”)创建的所有订单中,值为“ frontend”。我能够创建一个自定义列,但无法用我的字段的值填充它。

对于WooCommerce 3.0+,我正在关注this guide from SkyVerge

这将创建自定义字段,并将其自动分配给通过前端结帐的订单:

/* Add custom field for orders created from the front-end */
add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
function before_checkout_create_order( $order, $data ) {
    $order->update_meta_data( 'Handleby', 'frontend' );
}

这将创建自定义列:

/**
 * Add column "Handled By" on orders page to filter for front-end orders
 *
 * @param string[] $columns
 * @return string[] $new_columns
 */

function add_order_handleby_column_header($columns) {

  $new_columns = array();

  foreach($columns as $column_name => $column_info) {

        $new_columns[ $column_name ] = $column_info;

    // Create a new column named "Handled By" after the Status column
    if('order_status' === $column_name) {
      $new_columns['order_handleby'] = __('Handled By', 'my-textdomain');
    }

    }

    return $new_columns;
}
add_filter('manage_edit-shop_order_columns', 'add_order_handleby_column_header', 20);
/* End add "Handled By" column */

这是从“ Handleby”获取元数据的辅助功能:

/* Helper function used to get custom meta "Handleby" */
if(!function_exists('get_order_handleby_meta')) :

  /**
   * Function to get meta from an order
   *
   * @param \WC_Order $order the order
   * @param string $key the meta key
   * @param bool $single whether to get the meta as a single item. Defaults to 'true'
   * @param string $context if 'view' then the value will be filtered
   * @return mixed the order property
   */

  function get_order_handleby_meta($order, $key = '', $single = true, $context = 'edit') {

    // For WooCommerce 3.0 or later
    if(defined('WC_VERSION') && WC_VERSION && version_compare(WC_VERSION, '3.0', '>=')) {

      $value = $order->get_meta($key, $single, $context);

    } else {

      // Have $order->get_id() check here in case WC_VERSION isn't defined correctly
      $order_id = is_callable(array($order, 'get_id')) ? $order->get_id() : $order->id;
      $value    = get_post_meta($order_id, $key, $single);
    }

    return $value;
  }

endif;
/* End of helper function to get meta */

这将尝试使用元值填充列。我相信这是我做错了的地方:

/**
 * Adds "Handled By" column content to the orders page after the Status column
 *
 * @param string[] $column name of column being displayed
 */

function add_order_handleby_column_content($column) {
  global $post;

  if('order_handleby' === $column) {

    $order = wc_get_order($post->ID);
    $handleby = get_order_handleby_meta($order, 'Handleby');

        echo $handleby;
  }
}
add_action('manage_shop_order_posts_custom_column', 'add_order_handleby_column_content');
/* End of adding column content */

最后,这将为列设置样式:

/**
 * Adjust the appearance for the new "Handle By" column.
 */

function add_order_handleby_column_style() {

  $css = '.widefat .column-order_status, .widefat .column-order_handleby { width: 9%; }';
  wp_add_inline_style('woocommerce_admin_styles', $css);
}
add_action('admin_print_styles', 'add_order_handleby_column_style');

echo $handleby可能是我出问题了。

如果有人可以指出正确的方向,那将是最有帮助的。谢谢!

1 个答案:

答案 0 :(得分:0)

没关系,事实上,在清除缓存后它确实可以工作。不过,如果有人正在寻找类似的解决方案,我将在此处留下这篇文章。