Skip to content

Instantly share code, notes, and snippets.

@claudiosanches
Last active June 4, 2022 07:52
Show Gist options
  • Save claudiosanches/357fec20ff4802e259d4 to your computer and use it in GitHub Desktop.
Save claudiosanches/357fec20ff4802e259d4 to your computer and use it in GitHub Desktop.
WooCommerce - Add Order Again button to My Orders actions
<?php
/**
* Add order again button in my orders actions.
*
* @param array $actions
* @param WC_Order $order
* @return array
*/
function cs_add_order_again_to_my_orders_actions( $actions, $order ) {
if ( $order->has_status( 'completed' ) ) {
$actions['order-again'] = array(
'url' => wp_nonce_url( add_query_arg( 'order_again', $order->id ) , 'woocommerce-order_again' ),
'name' => __( 'Order Again', 'woocommerce' )
);
}
return $actions;
}
add_filter( 'woocommerce_my_account_my_orders_actions', 'cs_add_order_again_to_my_orders_actions', 50, 2 );
@nektobit
Copy link

nektobit commented Oct 4, 2016

@minemindmedia, something like this?

add_filter( 'woocommerce_my_account_my_orders_actions', 'custom_add_tracking', 10, 2 );
function custom_add_tracking($actions, $order) {
    $order_id = get_object_vars($order);
    $st = WC_Shipment_Tracking_Actions::get_instance();
    $tracking_items = $st->get_tracking_items( $order_id['id'], true );

    if ( $tracking_items )
        foreach ( $tracking_items as $tracking_item ) {
            $actions['track_link'] = array(
                    'url' =>  esc_url( $tracking_item[ 'formatted_tracking_link' ] ),
                    'name' => __( 'Track', 'woocommerce-shipment-tracking' )
            );
        }
    return $actions;
}

@bantunesm
Copy link

I added all the statusesn got something like this $order->has_status( 'completed' ) OR $order->has_status( 'processing' ) OR $order->has_status( 'cancelled' ) OR $order->has_status( 'failed' ) ) ....

Displaying OK
Linking NOT OK

It seems that woocommerce by default is not authorizing the re order function for the others statuses.

An idea to "fix" that ?

@heymarvin
Copy link

This method order_again does not carry over add-ons data.

@labto
Copy link

labto commented May 10, 2018

Hi!
Can I do the same thing but instead add the "download" button from the Order Attachment for Woocommerce extension:
https://wordpress.org/plugins/order-attachment-for-woocommerce/

Right now it shows up on the single-order page. Is it possible to do it?

@robbenz
Copy link

robbenz commented Sep 11, 2018

for woocommerce 3.4 change
$order->ID
to
$order->get_id()

@rulrulra
Copy link

Hi
How do I open a new window when I click a button instead of going to that page?
'url' => 'https://www.google.com'

@r0ks0n
Copy link

r0ks0n commented May 1, 2020

Hello,

Is there any way to skip cart page with this code and redirect user to checkout?

@Alexio454
Copy link

It would be better to also have the button on orders page next to view my order button. It's kind of getting lost where it's placed.
Do you think you could do that?

@Nyconing
Copy link

Use this to declare which statuses can have order again button.

add_filter( 'woocommerce_valid_order_statuses_for_order_again', 'add_order_again_status', 10, 1);
function add_order_again_status($array){
    $array = array_merge($array, array('on-hold', 'processing', 'pending-payment', 'cancelled', 'refunded'));
    return $array;
}

https://stackoverflow.com/questions/42116885/woocommerce-allowing-order-again-for-different-statuses

Copy link

ghost commented Jun 4, 2022

Hi all,
I have added this code to my functions.php. It works on orders with the status 'completed'. But I need to add this function to orders with status 'processing' also. So I added the below lines. But the link to which the button redirect shows the cart is empty. Could anyone help me, please?

elseif ( $order->has_status( 'processing') ) {
$actions['order-again'] = array(
'url' => wp_nonce_url( add_query_arg( 'order_again', $order->id ) , 'woocommerce-order_again' ),
'name' => __( 'Order Again', 'woocommerce' )
);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment