Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DeveloperWil/0decf69258a03d6a18eea8e8a6eac252 to your computer and use it in GitHub Desktop.
Save DeveloperWil/0decf69258a03d6a18eea8e8a6eac252 to your computer and use it in GitHub Desktop.
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

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 via email

@wez-dev
Copy link

wez-dev commented May 12, 2023

Thank you - helped me access the metadata and pass it to Stripe!

@MuggleWizard
Copy link

how do I add product name to the metadata? I'm able to write out a line: $metadata[ __( 'Event Title', 'woocommerce-gateway-stripe' ) ] = sanitize_text_field( $event_title['Event Name:']);
and am using $product = $product->get_product();
$event_title= $product->get_name();
Then i added $product to the parameters of the function and made it 10,4 for the filter. I'm new to this so I'm trying to piece it all together correctly. what am I doing wrong?

@DeveloperWil
Copy link
Author

DeveloperWil commented Nov 15, 2023

how do I add product name to the metadata? I'm able to write out a line: $metadata[ __( 'Event Title', 'woocommerce-gateway-stripe' ) ] = sanitize_text_field( $event_title['Event Name:']); and am using $product = $product->get_product(); $event_title= $product->get_name(); Then i added $product to the parameters of the function and made it 10,4 for the filter. I'm new to this so I'm trying to piece it all together correctly. what am I doing wrong?

If you want to add the product name as Stripe meta then add these two lines after Line 17 in my code above:

$product = $product->get_product();
$event_title = $product->get_name();
$metadata[  __( 'Event Title', 'woocommerce-gateway-stripe' )  ] = 'Event Title: ' . sanitize_text_field( $event_title );

@MuggleWizard
Copy link

MuggleWizard commented Nov 15, 2023 via email

@DeveloperWil
Copy link
Author

@MuggleWizard Okies, sorry - that was my bad.
You need to get the line items from the $order variable, even if there's only one item.

So, you can change line 29 of the original code to:
$metadata['Event Item '.$count] = 'Event name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. number_format( $item_total, 2 );

So you'll end up the "Event Item 1" as the stripe meta field.

You could do some extra processing beforehand to see if there is only one line item like:

$items = $order->get_items();
if ( count( $items ) === 1 ){
  $product = $line_item[0]->get_product();
  $event_title = $product->get_name();
  $metadata[  __( 'Event Title', 'woocommerce-gateway-stripe' )  ] = 'Event Title: ' . sanitize_text_field( $event_title );
} else {
  //do the line item iteration
}

You should probably do some testing about the product type and name before you run this code if you ever plan to offer products other than the event ticket,

Hop that helps.

@MuggleWizard
Copy link

MuggleWizard commented Nov 16, 2023 via email

@MuggleWizard
Copy link

I am now trying to get the product category from the order variable.

Here’s the latest code I was attempting that I found on stack overflow:

"As you mentioned get_categories() method of WC_Product class is deprecated. Instead you can use get_category_ids() method. However this method returns product category IDs, and It seems that you need category names so we can get the names from WP_Term objects.

Final code would be something like this:

add_action('woocommerce_checkout_create_order_line_item', 'add_order_item_custom_meta', 10, 4 );
function add_order_item_custom_meta($item, $cart_item_key, $cart_item, $order)
{
$product = $item->get_product(); // The WC_Product instance Object
$cat_ids = $product->get_category_ids(); // returns an array of cat IDs
$cat_names = [];

foreach ( (array) $cat_ids as $cat_id) {
    $cat_term = get_term_by('id', (int)$cat_id, 'product_cat');
    if($cat_term){
        $cat_names[] = $cat_term->name; // You may want to get slugs by $cat_term->slug
    }
}

$item->update_meta_data( '_raw_product_id', $product->get_id() );
$item->update_meta_data( '_raw_product_name', $cat_names );

};
"
I added this below your function for wbdc_filter_wc_stripe_payment_metadata but I’m getting an error processing message.
In researching the issue, I’ve found suggestions to use the $terms =wp_get_post_terms($post->ID, ‘product-cat’);
Or $terms = wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'names' ) );
I’ve tried adding these lines to your original code but I can’t seem to get them to work correctly. If you have any advice, I’d appreciate it. Thanks again for your help.

@bryanm1990
Copy link

Hi, I need help. The code worked very well for me until I updated the version of WooCommerce 5 days ago, since then it has not loaded the metadata in Stripe, not even an error message appears.

@DeveloperWil
Copy link
Author

Hi, I need help. The code worked very well for me until I updated the version of WooCommerce 5 days ago, since then it has not loaded the metadata in Stripe, not even an error message appears.

The hook has nothing to do with the WooCommerce plugin. The hook is in the WooCommerce Stripe Payment Gateway plugin https://en-au.wordpress.org/plugins/woocommerce-gateway-stripe/.

Here's the line of code with the hook https://github.com/search?q=repo%3Awoocommerce%2Fwoocommerce-gateway-stripe%20wc_stripe_payment_metadata&type=code.

WooCommere has been pushing hard for everyone to upgrade to the new payment gateway WooPayments. If you are using the new WooPayments plugin, there is no equivalent, sorry.

@progmartin
Copy link

thank you

@githubuser5789
Copy link

Hello. I am trying to follow your tutorial here. Am I in the right place for the code discussed? I did not see the direct github link in the video description.

https://www.youtube.com/watch?v=I82v_QOgt9Y

Screenshot 2024-03-28 192905

@DeveloperWil
Copy link
Author

Hello. I am trying to follow your tutorial here. Am I in the right place for the code discussed? I did not see the direct github link in the video description.

https://www.youtube.com/watch?v=I82v_QOgt9Y

Screenshot 2024-03-28 192905

Yes, this is the code repository.

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