Skip to content

Add Filter to Disable WooPay for Improved Compatibility #10468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/update-9938
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

New Filter: disable_woopay – Allows developers to conditionally disable WooPay based on custom logic, improving compatibility with themes and plugins.
23 changes: 23 additions & 0 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ public function get_form_fields() {
*/
public function init_hooks() {
add_action( 'init', [ $this, 'maybe_update_properties_with_country' ] );
add_action( 'init', [ $this, 'disable_woopay' ] );
// Only add certain actions/filter if this is the main gateway (i.e. not split UPE).
if ( self::GATEWAY_ID === $this->id ) {
add_action( 'woocommerce_order_actions', [ $this, 'add_order_actions' ] );
Expand Down Expand Up @@ -4434,6 +4435,28 @@ private function upe_needs_redirection( $payment_methods ) {
return 1 === count( $payment_methods ) && 'card' !== $payment_methods[0];
}


/**
* Disable WooPay.
*
* This function disables WooPay by updating the option to 'no'
* once the 'disable_woopay' filter is called.
*
* @return boolean False, indicating WooPay is disabled.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return boolean False, indicating WooPay is disabled.
* @return boolean False

false will always returned, whether WooPay has been disabled or not.

Is return false needed? Can the function just return void?

*/
public function disable_woopay() {
// Filter: disable_woopay.
$disable = apply_filters( 'disable_woopay', false );

// disable WooPay by updating the option to 'no'.
if ( $disable ) {
$this->update_option( 'platform_checkout', 'no' );
}

return false;
}
Comment on lines +4447 to +4457
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation creates a permanent change in the DB, which is not a good practice. Filters and actions should typically control behavior at runtime without causing permanent side effects.

For example, what would happen if a plugin hooking into disable_woopay on one request returns true but on the next request returns false?
Or what would happen if a plugin conditionally added the disable_woopay filter, like in the example below?

add_action( 'plugins_loaded', function () {
    // maybe a plugin would like to disable the WooPay button on product pages. But with the current implementation, it would disable WooPay on subsequent requests of checkout pages as well.
	if ( is_product() ) {
		add_filter( 'disable_woopay', '__return_true' );
	}
} );



/**
* Handles the shipping requirement for Afterpay payments.
*
Expand Down
Loading