Skip to content
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

introduce woocommerce_paypal_payments_product_supports_prb filter #353

Merged
merged 2 commits into from
May 10, 2022

Conversation

helgatheviking
Copy link
Contributor

@helgatheviking helgatheviking commented Nov 8, 2021

Description

Add a filter that will allow other plugins to conditionally disable the payment request buttons for single products that cannot support them.

Steps to test:

add_filter( 'woocommerce_paypal_payments_product_supports_prb', '__return_false' );

Should turn off payment request buttons on all single products even if they are enabled in the admin.

Because the $product object is passed, can also disable conditionally, for example, in the case of Mix and Match products

/**
 * Disable support for payment request buttons
 * 
 * @param bool $supports_prb
 * @param WC_Product $product
 * @return bool
 */
function kia_test_buttons( $supports_prb, $product ){
    if ( $product->is_type( 'mix-and-match' ) ) {
        $supports_prb = false;
    }
    return $supports_prb;
    
}
add_action( 'woocommerce_paypal_payments_product_supports_prb', 'kia_test_buttons', 10, 2 );

Changelog entry

Add woocommerce_paypal_payments_product_supports_prb filter

Closes #234.

cc @jimjasson

@helgatheviking
Copy link
Contributor Author

Hi team, can we get some eyes on this?

Copy link
Contributor

@leonardola leonardola left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution. I left a note about some changes that would make the hook more flexible for other plugins.

&& (
$product->is_type( array( 'external', 'grouped' ) )
|| ! $product->is_in_stock()
&& ( ! apply_filters( 'woocommerce_paypal_payments_product_supports_prb', ! $product->is_type( array( 'external', 'grouped' ) ) && $product->is_in_stock(), $product )
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better if we did apply_filters( 'woocommerce_paypal_payments_product_supports_prb', $product) instead. This would allow the plugin adding the filter to have access to more info in the product. Also we should add the apply filter as the last part of the if statement. So we don't call the hook unless the other checks passed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also I tend to dislike abbreviations as sometimes they usually need more context to understand. So I would suggest to change the hook name to woocommerce_paypal_payments_product_supports_payment_request_button even though it's quite long.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure I understand the first comment. Do you mean something like:

$supports = ! is_checkout() && is_a( $product, \WC_Product::class ) && ! $product->is_type( array( 'external', 'grouped' ) ) && $product->is_in_stock();
$supports = apply_filters( 'woocommerce_paypal_payments_product_supports_payment_request_button', $product ); 

if ( ! $supports ) {
	return;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops. I misread the ) here. So we are actually good to go. Just changing the filter name would be enough.

@leonardola leonardola self-assigned this May 9, 2022
@leonardola
Copy link
Contributor

I've tested this and I can still see the PayPal button in the cart when a product that does not support it is present. It should probably not show up there right?

@helgatheviking
Copy link
Contributor Author

It should probably not show up there right?

As far as I know, it's fine there. For example, in my Name Your Price plugin... the payment request buttons don't work on the single product page because there's an extra input where the user enters their price. So users can click the PRB before entering a price and bypass the minimum price checks! Ideally, I'd love to enable/disable the buttons when the price is valid, but that's not possible yet.

Once the Name Your Price product is in the cart though... it has a price. So in the cart the PRB don't cause any trouble.

Copy link
Contributor

@leonardola leonardola left a comment

Choose a reason for hiding this comment

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

Thanks @helgatheviking for contributing in this one. Keep rockin'

@leonardola leonardola merged commit e6775ce into woocommerce:trunk May 10, 2022
@helgatheviking
Copy link
Contributor Author

Glad to see this merged! @leonardola one more thing... what would be the easiest way to test this plugin is active? Like a class_exists( "something" ) or equivalent?

@leonardola
Copy link
Contributor

leonardola commented May 10, 2022

I usually check the class like you posted. But you can also use this function.

I'll probably do a release tomorrow so keep an eye open to update any code running on that hook on your sites

@leonardola
Copy link
Contributor

Just got pinged that we'll need to postpone the release to after May 20

@helgatheviking
Copy link
Contributor Author

No problem, I'm not in a rush. Which class would you test for? WooCommerce\PayPalCommerce ?

@leonardola
Copy link
Contributor

You could check this class as it seems to be loaded on every request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Request] Disable PayPal Smart buttons for specific product types
3 participants