Last active
January 27, 2023 16:32
-
-
Save InpsydeNiklas/95546cb7f82dc958cda0edeb4d78dd45 to your computer and use it in GitHub Desktop.
disable PayPal Payments (incl. PayPal Card Processing) for product with a given ID (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 a product with a given id | |
function ppcp_remove_single_product_buttons() { | |
$product = wc_get_product(); | |
if ($product) { | |
if ( $product->get_id() == 24 ) { | |
return false; | |
} | |
else { return true;} | |
} }; | |
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 product with a given id 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_products( $available_gateways ) { | |
// Not in backend (admin) | |
if( is_admin() ) | |
return $available_gateways; | |
// Set var default state | |
$disable_paypal_for_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 product with disabled buttons is present | |
if( $product->get_id() == 24 ) $disable_paypal_for_product = true; | |
} | |
// Remove PayPal Payments gateways for product with disabled buttons | |
if($disable_paypal_for_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_products', 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment