Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code example on how to filter available payment categories displayed in WooCommerce checkout for Klarna Payments plugin (https://wordpress.org/plugins/klarna-payments-for-woocommerce/).
/**
* Use together with Klarna Payments for WooCommerce.
* https://wordpress.org/plugins/klarna-payments-for-woocommerce/
*
* Filter the available payment categories displayed in checkout.
* Klarna Payments is registered as one single payment method in WooCommerce but can be displayed as multiple payment methods in checkout.
*
* Add this code to your themes functions.php file or include it in a separate functionality plugin (https://css-tricks.com/wordpress-functionality-plugins/).
**/
add_filter( 'wc_klarna_payments_available_payment_categories', 'krokedil_wc_klarna_payments_available_payment_categories' );
function krokedil_wc_klarna_payments_available_payment_categories( $available_payment_categories ) {
// Temporarily uncomment the row below and turn on WP_DEBUG_LOG (https://codex.wordpress.org/Debugging_in_WordPress) if you want to see what's returned in the $available_payment_categories array.
// error_log('$available_payment_categories ' . var_export( $available_payment_categories, true ) );
foreach( $available_payment_categories as $key => $payment_category ) {
// In this example we hide pay_over_time (Slice it) if cart total is below 20.
// pay_now, pay_later, and pay_over_time are commonly occuring identifiers.
if( 'pay_over_time' == $payment_category->identifier && WC()->cart->total < 20 ) {
unset( $available_payment_categories[$key]);
}
}
return $available_payment_categories;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment