Search code examples
phpwordpresswoocommercepaymentorders

Add "Pay for order" Button in WooCommerce My account Orders Custom Column


Based on the answer code by LoicTheAztec for Add a pay order button on WooCommerce My account view order for pending orders, I have added additional code in an attempt in getting this button to show up in a custom column directly on the "View orders" endpoint.

The column is there, but the button is not. I have tried switching $order with $order_id in the function as well, without success.

This is the code I am working with:

add_filter( 'woocommerce_my_account_my_orders_columns', 'add_payment_column_to_myaccount', 10, 1 );
function add_payment_column_to_myaccount( $columns ) {

    $new_columns = [];

    foreach ($columns as $key => $name){

        $new_columns[$key] = $name;

        if ('order-actions' === $key){

            $new_columns['pay-order'] = __('Payment', 'woocommerce');
        }
    }

    return $new_columns;
}

add_action( 'woocommerce_my_account_my_orders_column_order-items', 'add_pay_for_order_to_payment_column_myaccount', 10, 1);
function add_pay_for_order_to_payment_column_myaccount( $order ) {

    $order = wc_get_order( $order_id );

    if ( $order->get_status() == "pending" || $order->get_status() == "on-hold" ) {

        printf('<a class="woocommerce-button button pay" href="%s/order-pay/%s/?pay_for_order=true&key=%s">%s</a>',

        wc_get_checkout_url(), $order_id, $order->get_order_key(), __("Pay for this order", "woocommerce")
    );
    }
}

Solution

  • You missed pay-order in the composite hook from your 2nd function:

    woocommerce_my_account_my_orders_column_{$column_key}
    

    where $column_key need to be replaced by pay-order (but not order-items).

    There are also some other mistakes. Try the following:

    add_filter( 'woocommerce_my_account_my_orders_columns', 'add_payment_column_to_myaccount' );
    function add_payment_column_to_myaccount( $columns ) {
        $new_columns = [];
    
        foreach ($columns as $key => $name){
            $new_columns[$key] = $name;
    
            if ('order-actions' === $key){
                $new_columns['pay-order'] = __('Payment', 'woocommerce');
            }
        }
        return $new_columns;
    }
    
    add_action( 'woocommerce_my_account_my_orders_column_pay-order', 'add_pay_for_order_to_payment_column_myaccount' );
    function add_pay_for_order_to_payment_column_myaccount( $order ) {
        if( in_array( $order->get_status(), array( 'pending', 'on-hold' ) ) ) {
            printf( '<a class="woocommerce-button button pay" href="%s/order-pay/%s/?pay_for_order=true&key=%s">%s</a>',
                wc_get_checkout_url(), $order->get_id(), $order->get_order_key(), __("Pay for this order", "woocommerce")
            );
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.