How to enable WooCommerce Multiple Stripe Accounts?

In WooCommerce, you can connect with the WooCommerce Stripe Payment Gateway. Doing so, one can accept credit card payment made through this free Stripe payment gateway plugin. You need to activate “Live Publishable Key” and “Live Secret Key” for that matter. After that, you can use your Stripe account for different payments. However, you need to code a few things to switch Stripe programmatically and conditionally. So, in this article, you will learn to enable WooCommerce multiple stripe accounts. 

After you activate this snippet, you can utilize multiple Stripe accounts with a single WooCommerce installation. For instance, there will be two accounts for accepting payments for digital products & physical products.

Generally, you require to create a custom class to utilize online documentation & snippets. It is often a difficult task in programming. Alternatively, you can use WooCommerce Stripe hooks to access multiple Stripe accounts in a single WooCommerce installation.

Read further to use different stripe accounts for product IDs. However, go through the disclaimer first to avoid potential bad consequences. 

Image showing stripe keys that will be used to enable WooCommerce multiple stripe accounts

PHP Snippet: Enable WooCommerce multiple stripe accounts for Product ID at Checkout page

Caution: Using this snippet can have potential side effects. It can disturb the Stripe refund handling process or cause other issues. The best way is to manually create Stripe refunds rather than using WooCommerce order admin. It is risky to customize payment gateways & should be done if you are aware of potential threats.

Please note that the below snippet switches Stripe keys after seeing the product ID. So, if your order contains multiple products, then you will need to figure out another way. Under such circumstances, you can allow single product in the cart. 

// -------------------
// 1. Create function to find Product ID
 
function phpsof_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', 'phpsof_conditional_publishable_key', 9999 );
 
function phpsof_conditional_publishable_key( $params ) {
 
   // PRODUCT ID HERE
   if ( ! phpsof_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', 'phpsof_conditional_publishable_key_request', 9999 );
 
function phpsof_conditional_publishable_key_request( $params ) {
 
   // PRODUCT ID HERE
   if ( ! phpsof_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', 'phpsof_conditional_private_key_headers', 9999 );
 
function phpsof_conditional_private_key_headers( $headers_args ) {
 
   // PRODUCT ID HERE
   if ( ! phpsof_product_id_in_cart( 12345 ) ) return $headers_args;
 
   // STRIPE Live Secret Key HERE
   $headers_args[ 'Authorization' ] = 'Basic ' . base64_encode( 'sk_live_..........' . ':' );
 
   return $headers_args;
}

Where do you add this snippet?

So, this is how you enable WooCommerce multiple stripe accounts for product ID or category slug. Also, it is easier to activate this function with this code. So, to apply this code, just add this PHP snippet at your child theme function.php file’s bottom. It should be placed before “?>”, if you have it there. Apart from this, CSS goes in your child theme style.css file. Make sure that you are editing these files in a right manner for best results.

Is this snippet still valid?

So, this way, you can enable WooCommerce multiple stripe accountsI have applied this code on the Storefront theme and WordPress friendly hosting PHP 7.3. Let me know if everything works as expected when you code. Share it further if you find this snippet useful for you and it had saved your time. 

Also Read, How to Exclude Product from Discount Coupons in WooCommerce?

How to Alter WooCommerce Order Field after Checkout?

Important links: WooCommerce – https://woocommerce.com/

Stripe – https://stripe.com/en-in