Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce: Stripe product and customer metadata
/**
* Add Stripe metadata along with WooCommerce purchase
*
* @param $metadata
* @param $order
* @param $source
* @return mixed
*/
function wbdc_filter_wc_stripe_payment_metadata( $metadata, $order, $source ) {
/**
* Get order data
*/
$order_data = $order->get_data();
$metadata[ __( 'Billing Company', 'woocommerce-gateway-stripe' ) ] = sanitize_text_field( $order_data['billing']['company'] );
$metadata[ __( 'Customer Name', 'woocommerce-gateway-stripe' ) ] = sanitize_text_field( $order_data['billing']['first_name'] . ' ' . $order_data['billing']['last_name'] );
$metadata[ __( 'Customer Phone', 'woocommerce-gateway-stripe' ) ] = sanitize_text_field( $order_data['billing']['phone'] );
/**
* List products purchased
*/
$count = 1;
foreach( $order->get_items() as $item_id => $line_item ){
$item_data = $line_item->get_data();
$product = $line_item->get_product();
$product_name = $product->get_name();
$item_quantity = $line_item->get_quantity();
$item_total = $line_item->get_total();
$metadata['Line Item '.$count] = 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. number_format( $item_total, 2 );
$count += 1;
}
return $metadata;
}
add_filter( 'wc_stripe_payment_metadata', 'wbdc_filter_wc_stripe_payment_metadata', 10, 3 );
@harryfine
Copy link

harryfine commented Oct 29, 2021

You are an ace. I was having trouble getting the metadata of product code back after making a sale from Woocommerce. This code worked perfectly. I appreciate the time you took writing this and thank you for providing it for free.

@DeveloperWil
Copy link
Author

DeveloperWil commented Oct 30, 2021

@rexlum
Copy link

rexlum commented Feb 14, 2022

Hi!
Thank you for the code it worked amazing. The only issue that I have now is that it took many efforts to get it exactly is I wanted and each effort added a column to the table when CSV is exported from stripe. Now those columns are empty and the table looks really messy. Is there a way I can exclude those empty columns from the payment report from woocommerce with an if statement perhaps? Thank you in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment