The WooCommerce plugin fully integrates with the WooCommerce Stripe Payment Gateway plugin, developed by Automattic itself. With this free plugin, Stripe payment gateway can be enabled via the WooCommerce settings and once your Stripe “Live Publishable Key” and “Live Secret Key” are set, your WooCommerce shop is ready to take credit card payments powered by Stripe.
Now, there is some documentation online which explains, with a little bit of code, how to switch Stripe account programmatically and conditionally i.e. for a given product ID or product category slug – same as what we’ve seen recently with PayPal Standard (here’s the tutorial for using different PayPal accounts inside a single WooCommerce installation). For example, you may want to use a Stripe account for digital sales and a different one for physical products.
Unlike PayPal Standard, however, online documentation and snippets are quite out of date and require, often, to create a custom Class which is always a difficult task in PHP programming. Thankfully, there are new WooCommerce Stripe hooks and therefore it’s possible to use different / multiple Stripe accounts in a single WooCommerce installation.
Please read the disclaimer below and – only then – enjoy!
PHP Snippet: Use Different Stripe Account For a Product ID @ WooCommerce Checkout
/**
* @snippet Switch Stripe Account @ WooCommerce Checkout
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire, BusinessBloomer.com
* @testedwith WooCommerce 4.6
* @donate $9 https://tutoraspire.com
*/
// -------------------
// 1. Create function to find Product ID
function tutoraspire_product_id_in_cart( $id ) {
$product_cart_id = WC()->cart->generate_cart_id( $id );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) return true;
return false;
}
// -------------------
// 2. Change Stripe keys on the go
add_filter( 'wc_stripe_params', 'tutoraspire_conditional_publishable_key', 9999 );
function tutoraspire_conditional_publishable_key( $params ) {
// PRODUCT ID HERE
if ( ! tutoraspire_product_id_in_cart( 12345 ) ) return $params;
// STRIPE Live Publishable Key HERE
$params[ 'key' ] = 'pk_live_................';
return $params;
}
add_filter( 'wc_stripe_payment_request_params', 'tutoraspire_conditional_publishable_key_request', 9999 );
function tutoraspire_conditional_publishable_key_request( $params ) {
// PRODUCT ID HERE
if ( ! tutoraspire_product_id_in_cart( 12345 ) ) return $params;
// STRIPE Live Publishable Key HERE
$params[ 'stripe' ][ 'key' ] = 'pk_live_................';
return $params;
}
add_filter( 'woocommerce_stripe_request_headers', 'tutoraspire_conditional_private_key_headers', 9999 );
function tutoraspire_conditional_private_key_headers( $headers_args ) {
// PRODUCT ID HERE
if ( ! tutoraspire_product_id_in_cart( 12345 ) ) return $headers_args;
// STRIPE Live Secret Key HERE
$headers_args[ 'Authorization' ] = 'Basic ' . base64_encode( 'sk_live_..........' . ':' );
return $headers_args;
}