we have the situation that for some products the smart buttons (direct payment with bypassing the classical checkout) must be disabled in the shopping basket and the checkout page.
It took me a bit to test this properly, but I was able to confirm a working approach.
Yes, there is a filter you can use to conditionally disable Smart (express) buttons based on products in the cart. In the example below, the PayPal buttons are removed from both the cart and checkout when a specific product ID is present (173 in this case).
add_filter(
'woocommerce_paypal_payments_selected_button_locations',
function ( array $locations ): array {
// Bail early if the cart is not available yet.
if ( ! function_exists( 'WC' ) || ! WC()->cart ) {
return $locations;
}
$restricted_product_id = 173;
$product_in_cart = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if (
(int) $cart_item['product_id'] === $restricted_product_id ||
(int) $cart_item['variation_id'] === $restricted_product_id
) {
$product_in_cart = true;
break;
}
}
if ( $product_in_cart ) {
// Remove the cart and checkout locations so no smart/express
// buttons are rendered when the product is in the basket.
$locations_to_disable = [ 'cart', 'checkout', 'checkout-block-express' ];
$locations = array_values(
array_filter(
$locations,
fn( string $loc ) => ! in_array( $loc, $locations_to_disable, true )
)
);
}
return $locations;
}
);
GIF above with test. This will ensure that express buttons are not rendered at all in those contexts when the defined product is present in the cart.
If this helped and you’re happy with the support, feel free to leave a quick review on WordPress. It means a lot to us and shows that we are needed as support.