Last active
July 22, 2022 19:02
-
-
Save InpsydeNiklas/ed7c3d9579902859c51023579caad0c6 to your computer and use it in GitHub Desktop.
disable PayPal Payments (incl. PayPal Card Processing) for subscription type products with Vaulting enabled (mini cart button may still render)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// removes PayPal smart buttons on single product page for subscription type products | |
function ppcp_remove_single_product_buttons( $enable_button, $product ){ | |
if ( $product->is_type( 'subscription' ) ) { | |
$enable_button = false; | |
} | |
return $enable_button; | |
} | |
add_action( 'woocommerce_paypal_payments_product_supports_payment_request_button', 'ppcp_remove_single_product_buttons', 10, 2 ); | |
// removes the PayPal and PayPal Card Processing gateways when a subscription product is in the cart | |
// disabling the PayPal gateway will cause the button to also be hidden on the Cart page | |
// the Mini Cart button may render regardless | |
function remove_ppcp_gateways_for_subscriptions( $available_gateways ) { | |
// Not in backend (admin) | |
if( is_admin() ) | |
return $available_gateways; | |
// Set var default state | |
$subscription_product = false; | |
// Only execute loop on cart and checkout page | |
if ( is_cart() || is_checkout() ) { | |
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
// Get the WC_Product object | |
$product = wc_get_product($cart_item['product_id']); | |
// Determine if subscription product is present | |
if($product->is_type('subscription')) $subscription_product = true; | |
} | |
// Remove PayPal Payments gateways for subscription products | |
if($subscription_product) | |
unset($available_gateways['ppcp-gateway']); // unset 'ppcp-gateway' - PayPal | |
unset($available_gateways['ppcp-credit-card-gateway']); // unset 'ppcp-credit-card-gateway' - PayPal Card Processing | |
} | |
return $available_gateways; | |
} | |
add_filter('woocommerce_available_payment_gateways', 'remove_ppcp_gateways_for_subscriptions', 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment