Jeil
Jeil

Reputation: 79

Remove cancel button from WooCommerce My account Orders conditionally

I want to make sure the cancel button is not visible in my-account> my-order when 'Payment Method Title' is 'Npay'.

The 'Npay' is an external payment gateway and does not work with the commerce. Therefore, payment cancellation must be done externally only.

add_filter('woocommerce_my_account_my_orders_actions', 'remove_my_cancel_button', 10, 2);
function remove_my_cancel_button($actions, $order){
    if ( $payment_method->has_title( 'Npay' ) ) {
        unset($actions['cancel']);
        return $actions;
    }
}

Upvotes: 2

Views: 3425

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253978

To remove the cancel button from My account Orders, we use the following:

add_filter('woocommerce_my_account_my_orders_actions', 'remove_myaccount_orders_cancel_button', 10, 2);
function remove_myaccount_orders_cancel_button( $actions, $order ){
    unset($actions['cancel']);

    return $actions;
}

But to remove the cancel button from My account Orders based on the payment title, you will use the WC_Order method get_payment_method_title() like:

add_filter('woocommerce_my_account_my_orders_actions', 'remove_myaccount_orders_cancel_button', 10, 2);
function remove_myaccount_orders_cancel_button( $actions, $order ){
    if ( $order->get_payment_method_title() === 'Npay' ) {
        unset($actions['cancel']);
    }
    return $actions;
}

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

The main variable argument $actions need to be returned at the end outside the IF statement

Upvotes: 6

Related Questions