Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/df4892df03c0299f6680f98384533eb5 to your computer and use it in GitHub Desktop.
Save dwanjuki/df4892df03c0299f6680f98384533eb5 to your computer and use it in GitHub Desktop.
Include subscription metadata in Stripe Checkout session parameters
<?php // copy from below
/**
* Include metadata in Stripe Checkout session params.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_stripe_checkout_params_send_user_data( $checkout_session_params ) {
$metadata = array();
// Get value of my_field from $_REQUEST.
if ( isset( $_REQUEST['my_field'] ) ) {
$metadata['my_field'] = sanitize_text_field( $_REQUEST['my_field'] );
}
// Bail if there's no metadata to add.
if ( empty( $metadata ) ) {
return $checkout_session_params;
}
// Add metadata to Stripe checkout session data.
if ( ! empty( $checkout_session_params['subscription_data'] ) ) {
$checkout_session_params['subscription_data']['metadata'] = $metadata;
} elseif ( ! empty( $checkout_session_params['payment_intent_data'] ) ) {
$checkout_session_params['payment_intent_data']['metadata'] = $metadata;
}
return $checkout_session_params;
}
add_filter( 'pmpro_stripe_checkout_session_parameters', 'my_pmpro_stripe_checkout_params_send_user_data' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment