自定义操作按钮到 WooCommerce 管理员订单列表上的自定义列

Custom action button into a custom column on WooCommerce admin orders list

首先,我做了一个自定义栏目。

  function add_example_column($columns) {
    $columns['EXAMPLE'] = 'EXAMPLE';
    return $columns;
  }

  add_filter( 'manage_edit-shop_order_columns', 'add_example_column' );

之后,我做了一个新动作

 function example_action($actions) {
    $actions['example'] = array (
      'url'     => 'https://example.com?action=ups',
      'name'    => __( 'Some text', 'woocommerce' ),
      'action'  => 'example'
    );
    return $actions;
  }

 add_action( 'woocommerce_admin_order_actions', 'example_action', 10, 1 );

然后,我尝试将此操作按钮放到自定义列中。

  function example_barcode($column, $order_id) {
    $order = new WC_Order( $order_id );
    if ( $column == 'EXAMPLE') :
      if ( $order->has_status( array( 'processing' ) ) ) :
        echo '<style>.wc-action-button-ups::after { font-family: FontAwesome !important; content: "\f7e0" !important; }</style>';
      endif;
    endif;
  }

  add_action( 'manage_shop_order_posts_custom_column', 'example_barcode', 10, 2 );

仍然没有成功。

您的代码中有一些错误。要将自定义操作按钮添加到管理员订单列表的自定义附加列中,请使用以下内容:

// Adding a custom comumn
add_filter( 'manage_edit-shop_order_columns', 'add_example_column' );
function add_example_column($columns) {
    $columns['ups'] = __('UPS', 'woocommerce');
    return $columns;
}

// The column content by row
add_action( 'manage_shop_order_posts_custom_column' , 'add_example_column_contents', 10, 2 );
function add_example_column_contents( $column, $post_id ) {
    if ( 'ups' === $column )
    {
        $order = wc_get_order( $post_id ); // Get the WC_Order instance Object

        // Targetting processing orders only
        if ( $order->has_status( 'processing' ) )
        {
            $slug = 'ups';
            $url  = '?action=ups&order_id=' . $post_id; // The order Id is required in the URL

            // Output the button
            echo '<p><a class="button wc-action-button wc-action-button'.$slug.' '.$slug.'" href="'.$url.'" aria-label="'.$slug.'"> </a></p>';
        }
    }
}

// The CSS styling
add_action( 'admin_head', 'add_custom_action_button_css' );
function add_custom_action_button_css() {
    $action_slug = "ups";

    echo '<style>.wc-action-button-'.$action_slug.'::after { font-family: woocommerce !important; content: "\e029" !important; }</style>';
}

但是您不会像默认的 WooCommerce 操作列中那样获得工具提示功能。

您将不得不通过一些额外的功能来处理您的自定义操作。

注意: 挂钩 woocommerce_admin_order_actions 将与默认自定义 woocommerce 按钮操作一起使用,因此不适用于自定义列。此外,FontAwesome 似乎无法在后端运行。

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。