Skip to content

Instantly share code, notes, and snippets.

@InpsydeNiklas
Created October 17, 2022 16:03
Show Gist options
  • Save InpsydeNiklas/1c873948930cf8235d40e458fcaab338 to your computer and use it in GitHub Desktop.
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
<?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