Force Save Payment Method
Some businesses need to collect payment information during checkout but charge the customer's payment method at a later time. This is particularly common for merchants with long fulfillment timelines, custom manufacturing processes, or complex supply chains where products aren't immediately available for shipping.
Since payment authorizations typically expire within 7 days, you need a way to securely store the customer's payment method for future charging. This code sample automatically saves every customer's payment method during checkout, ensuring you can process payments weeks or months later when orders are ready to fulfill.
When would you use this?
Forcing payment method storage is essential for businesses that can't complete orders within standard authorization timeframes. Instead of relying on short-term payment holds, you can securely vault payment methods and charge them when fulfillment actually occurs.
Common use cases include custom or made-to-order products with extended manufacturing times, pre-orders for items not yet in stock, drop-shipping arrangements where supplier lead times are unpredictable, or subscription services that need reliable payment methods for future billing cycles.
The Code
This example forces all payment methods to be saved during the checkout process, regardless of whether customers check the "save payment method" box:
/**
* Force payment methods to be saved during checkout
* @param bool $bool Whether to save the payment method
* @param WC_Order $order The order object
* @return bool Always returns true to force saving
*/
add_filter('wc_stripe_force_save_payment_method', function($bool, $order){
if(is_checkout()){
$bool = true;
}
return $bool;
}, 10, 2);
How the Code Works
The code uses the wc_stripe_force_save_payment_method
filter, which runs during the checkout process when Stripe determines whether to save a customer's payment method. This filter receives two parameters: a boolean indicating the current save status, and the WooCommerce order object containing all order details.
The function checks if the current page is the checkout page using is_checkout()
. When this condition is true, it overrides any existing save preference by setting the boolean to true
, ensuring that every payment method entered during checkout is automatically saved to the customer's account in Stripe's secure vault.
Customizing the Save Conditions
You can modify when payment methods are automatically saved based on specific business logic. Here are some common variations:
Save only for orders above a certain value:
add_filter('wc_stripe_force_save_payment_method', function($bool, $order){
if(is_checkout() && $order->get_total() > 200){
$bool = true;
}
return $bool;
}, 10, 2);
Save for specific product categories:
add_filter('wc_stripe_force_save_payment_method', function($bool, $order){
if(is_checkout()){
foreach($order->get_items() as $item){
$product = $item->get_product();
if($product && in_array(25, $product->get_category_ids())){
$bool = true;
break;
}
}
}
return $bool;
}, 10, 2);
Save for registered customers only:
add_filter('wc_stripe_force_save_payment_method', function($bool, $order){
if(is_checkout() && is_user_logged_in()){
$bool = true;
}
return $bool;
}, 10, 2);
Important Considerations
When you force payment method saving, customers' payment information will be stored even if they didn't explicitly opt-in to save it. Make sure your privacy policy and checkout page clearly communicate that payment methods will be saved for future use, and consider adding explanatory text about why this is necessary for your business model.
Saved payment methods create ongoing PCI compliance responsibilities, as you're now storing tokenized payment data. While Stripe handles the actual card storage securely, ensure your checkout process clearly communicates the payment method storage to customers.
Customer Communication
Since you're overriding the customer's save preference, it's important to communicate this clearly during checkout. Consider adding explanatory text near the payment form:
// Add checkout notice explaining payment method storage
add_action('woocommerce_checkout_before_customer_details', function(){
if(!is_admin() && is_checkout()){
wc_print_notice('Your payment method will be securely saved for future billing when your order ships.', 'notice');
}
});
Testing Your Implementation
To verify the code is working correctly, complete a test checkout without checking the "save payment method" box. After the order is complete, check the customer's account page or your Stripe dashboard to confirm the payment method was saved despite not being explicitly selected by the customer.
You can also add logging to track when payment methods are being force-saved:
add_filter('wc_stripe_force_save_payment_method', function($bool, $order){
if(is_checkout()){
$bool = true;
wc_stripe_log_info('Payment method force-saved during checkout for enhanced fulfillment workflow');
}
return $bool;
}, 10, 2);
Remember to update your checkout page messaging and privacy policy to reflect that payment methods will be automatically saved before deploying this code to your live site.