Search code examples
phpjquerywordpresswoocommerceorders

Move an action button first in WooCommerce My account Orders page


Is it possible to always keep a specific action button first in the WooCommerce My Account Orders page?

I have a plugin which creates a "track" button once the order is shipped, and I'd like that button to always be first in the list, as you see it appears second.

screenshot

and the HTML code of that Track button:

<a href="tools.usps.com/go/…" class="woocommerce-button button ast_track">Track</a>

I tried to use the following jQuery code with no luck:

jQuery(document).ready(function($) {
    $('.woocommerce-MyAccount-navigation .track').prependTo('.woocommerce-MyAccount-navigation');
});

Solution

  • Try the following (unstested) based on this thread, to move "track" button first:

    add_filter( 'woocommerce_my_account_my_orders_actions', 'my_account_orders_track_button_first', 100, 2 );
    function my_account_orders_track_button_first( $actions, $order ) {
        // Targeting "track" params (button)
        if( is_wc_endpoint_url( 'orders' ) && isset($actions['ast_track']) ) {
            // Save "track" params to a variable
            $action_track = $actions['ast_track']; 
    
            // Remove "track" params from $actions array
            unset($actions['ast_track']); 
    
            // Add back "track" params first in the $actions array
            $actions = array_merge(['ast_track' => $action_track], $actions); 
        }
        return $actions;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.