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

时间:2020-01-10 20:52:23

标签: php wordpress woocommerce hook-woocommerce orders

首先,我创建了一个自定义列。

  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 );

仍然没有成功。

1 个答案:

答案 0 :(得分: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文件中。经过测试,可以正常工作。

相关问题