Created
October 17, 2022 16:03
-
-
Save InpsydeNiklas/1c873948930cf8235d40e458fcaab338 to your computer and use it in GitHub Desktop.
disable the Pay upon Invoice gateway for specific product IDs - WooCommerce PayPal Payments
This file contains hidden or 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 | |
function remove_ppcp_pui_for_products( $available_gateways ) { | |
// Not in backend (admin) | |
if( is_admin() ) | |
return $available_gateways; | |
// Set var default state | |
$excluded_pui_product = false; | |
// Only execute loop on checkout page | |
if ( 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']); | |
$id = $product->get_id(); | |
// Determine if excluded PUI product is present | |
if( $id === 16 ) $excluded_pui_product = true; | |
} | |
// Remove PayPal Payments gateways for excluded PUI products | |
if( $excluded_pui_product ) | |
// unset Pay upon Invoice gateway | |
unset($available_gateways['ppcp-pay-upon-invoice-gateway']); | |
} | |
return $available_gateways; | |
} | |
add_filter('woocommerce_available_payment_gateways', 'remove_ppcp_pui_for_products', 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment