Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
618 views
in Technique[技术] by (71.8m points)

Adding custom action button in Woocommerce admin orders list

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:

Admin orders list

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...