How to remove an action within a class with extends

There are two things that you’ll need in order to remove the action.

  1. The exact instance of WC_Gateway_Paypal
  2. Knowing when the action is added and when it is executed

Without these, it will be difficult, but impossible to remove the action. Getting the instance of WC_Gateway_Paypal should be straightforward.

//* Make sure class-wc-payment-gateways.php is included once
include_once PATH_TO . class-wc-payment-gateways.php;

//* Get the gateways
$gateways = \WC_Payment_Gateways::instance();

//* The class instances are located in the payment_gateways property
$gateways->payment_gateways

The Paypal gateway instance should be in this array. I don’t have WooCommerce installed on this machine right now, so I don’t know exactly how it’s stored. But you should be able to figure it out.

[Added: Thanks to @willington-vega for finding out how WC stores the payment gateways in the array. I’ve updated the get_wc_paypal_payment_gateway() function below with this.]

Fortunately, the action is added at priority 10 to the woocommerce_order_status_on-hold_to_completed hook. What we can do is hook in at an earlier priority and remove the hook.

add_action( 'woocommerce_order_status_on-hold_to_completed', 'remove_PaypalCapture_action', 9 );
function remove_PaypalCapture_action() {
  //* This is where we'll remove the action

  //* Get the instance
  $WC_Paypal_Payment_Gateway = get_wc_paypal_payment_gateway();
  remove_action( 'woocommerce_order_status_on-hold_to_completed', [ $WC_Paypal_Payment_Gateway , 'capture_payment' ] );
}
function get_wc_paypal_payment_gateway() {

  //* Make sure class-wc-payment-gateways.php is included once
  include_once PATH_TO . class-wc-payment-gateways.php;

  //* Return the paypal gateway
  return \WC_Payment_Gateways::instance()->payment_gateways[ 'paypal' ];
}

Leave a Comment