Backend orders list custom action buttons in Woocommerce 3.3+

后端 未结 2 1142
心在旅途
心在旅途 2021-01-07 01:59

Since WooCommerce version 3.3+ the code below that displays a custom action button in admin order list, doesn\'t work anymore.

// Add your custom order acti         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-07 02:22

    Here is the correct way to get this working, as this was the code from one of my answers, that will load in a separate browser window (or tab) the corresponding tracking page.

    hook woocommerce_admin_order_actions_end still exist and works. What has changed in vesion 3.3+ is the function that displays the buttons wc_render_action_buttons() and so the displayed buttons html structure and classes too.
    Why? … Because that order list display has been enhanced in version 3.3+.

    The code:

    // Add your custom order action button
    add_action( 'woocommerce_admin_order_actions_end', 'add_custom_order_actions_button', 100, 1 );
    function add_custom_order_actions_button( $order ) {
    
        // Get the tracking number
        $traking_number = get_post_meta( $order->get_id(), '_aftership_tracking_number', true );
        if( empty($traking_number) ) return;
    
        // Prepare the button data
        $url    = esc_url('https://track.aftership.com/'.$traking_number.'?');
        $name   = esc_attr( __('Tracking', 'woocommerce' ) );
        $class  = esc_attr( 'tracking' );
    
        // Custom action button (with a target='_blank' opening a new browser window)
        printf( '%s', $class, $class, $url, $name, $name );
    }
    
    // The icon of your action button (CSS)
    add_action( 'admin_head', 'add_custom_order_actions_button_css' );
    function add_custom_order_actions_button_css() {
        echo '';
    }
    

    Code goes in function.php file of your active child theme (or theme).

    Tested and works only for woocommerce version 3.3+

    Here I don't use woocommerce_admin_order_actions usual action hook, but instead I use an unusual hook, to allow displaying the tracking page in a separate browser window (or tab)

提交回复
热议问题