I've managed to create a custom column in Woocommerce admin orders page (Thanks to LoicTheAztec):
// Adding a custom column
add_filter( 'manage_edit-shop_order_columns', 'add_example_column' );
function add_example_column($columns) {
$columns['my_column'] = __('My Column', 'woocommerce');
return $columns;
}
// Show some content inside the column
add_action( 'manage_shop_order_posts_custom_column' , 'add_example_column_contents', 10, 2 );
function add_example_column_contents( $column, $post_id ) {
if ( 'my_column' === $column )
{
$order = wc_get_order( $post_id ); // Get the WC_Order instance Object
// Targetting completed orders only
if ( $order->has_status( 'completed' ) )
{
$slug = 'my_column';
$url = '?action=my_column&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.'">Create Invoice</a></p>';
}
}
if( isset( $_GET['action'] ) && isset( $_GET['order_id'] ) && $_GET['action'] === 'the_column' ) {
//
}
}
// The CSS styling
add_action( 'admin_head', 'add_custom_action_button_css' );
function add_custom_action_button_css() {
$action_slug = "my_column";
echo '<style>.wc-action-button-'.$action_slug.'::after { font-family: woocommerce !important; content: "e029" !important; }</style>';
}
Now the interface is looking like this:
All I need now is to create action for the "Create Invoice" buttons so when I click, the order's content will be POST-ed through an API using cURL on an internet address. I've managed to extract the info from the order and put it in JSon formatted cURL postfields along with the rest of the required data, all I need is to make the action happening, with a final success/failure pop-up window (no redirection).
question from:
https://stackoverflow.com/questions/65938913/adding-custom-action-button-in-woocommerce-admin-orders-list 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…