25 thoughts on “How To Add Custom Metadata To WooCommerce Stripe Purchase – WPQuickies”

  1. Hi Wil, thanks so much for this code, it’s super helpful! I’m wondering if you know how to send shipping type & cost from Woo to Stripe as a second Line Item Metadata or similar?

  2. César Hernández

    Hello and thank you very much for this article.

    Is there any way to do the reverse process, how to store the stripe payment information (4 last digits, credit card type) in the woocommerce order meta data?

    Thank you so much

    1. Hi Cesar
      There’s no official hook or filter supplied by the WooCommerce Stripe gateway plugin to get credit card details and that’s for security reasons.

      Technically this is possible if you have the stripe customer number, you can then retrieve the cards associated with the Stripe customer using https://stripe.com/docs/api/cards/retrieve.

      Then you could have access to the Card object https://stripe.com/docs/api/cards/object

      {
      "id": "card_1JtKLVEJ34ZLkWWBUdXnk1PL",
      "object": "card",
      "address_city": null,
      "address_country": null,
      "address_line1": null,
      "address_line1_check": null,
      "address_line2": null,
      "address_state": null,
      "address_zip": null,
      "address_zip_check": null,
      "brand": "Visa",
      "country": "US",
      "customer": "cus_E2cvrBecka5mCX",
      "cvc_check": "pass",
      "dynamic_last4": null,
      "exp_month": 8,
      "exp_year": 2022,
      "fingerprint": "axCDBI1gfJ36VZEi",
      "funding": "credit",
      "last4": "4242",
      "metadata": {},
      "name": null,
      "tokenization_method": null
      }

      The Card object stores the “brand” and the “last4” card digits.

      It’s a bit of work but possible, however, storing credit card numbers, even the last few digits, opens you up to liability and possible criminal charges should that information be leaked from your site.

      Regards,
      Wil.

  3. Hey Wil!,

    Is there anyway to pass description parameter to stripe payment gateway.
    Actually, business is registered in India & want to accept non-INR payment.
    For that getting the error on my checkout page.

    “As per Indian regulations, export transactions require a description. More info here: https://stripe.com/docs/india-exports“”

      1. Thank you Wil for you response.
        But not luck still getting same error.
        But after adding webhook endpoint to it. Error got changed.
        It seems like now I need to pass customer name, billing shipping address & description to the payment api.
        I was wandering if you can help me out with this.
        https://prnt.sc/1z00xnw.

  4. Hi Wil, thank you so much for this code, it’s really helpful.
    But the only problem is that the code is working when anyone is logged in and make the payment.
    In my site I am allowing customers to place orders as a guest user.
    For guest checkout, the metadata’s are not showing up in the captured payment history on Stripe metadata section after the successful payment.
    Could you please help me out on this?

    Thanks,
    Mrinal

    1. Interesting, let me test that out.

      There’s nothing in the code that requires a user to be logged in or out. It simply hooks into the Stripe transaction.

      Has this only happened the one time or multiple times?

      Wil.

  5. Hi,
    I am using woo Commerce plugin for wordpress for my Store. Integrated Stripe payment gateway, i have not find any way in WooCommerce to send tax related information against transaction. That will display in stripe tax report section? Is there anyway using wooCommerce i sent information to stripe tax report?

    1. Hi Umair

      Do you have “Enable Taxes” checked in the WooCommerce > Settings > General?

      Which countries do you have a tax class for?

      Do you have an order example that was sent through Stripe card payments that had tax included?

      Wil.

  6. Hello,

    I want to know the discount PER ITEM. How can I do it. I have searched a lot. Please help!

    Thanks!

  7. Hi,
    I want to pass product as event name, and sku as event id, order id as normal
    for example:
    Event Name: xxxyyy(product name)
    Event ID: 121332(SKU)
    Order id as normal: 123123

    Is it possible ?
    Please help me

    1. Yes, you can add any data that is stored in the database to the function.

      Find out where the data is stored in the database and how to extract it and assign it to a variable, then add it to the metadata variable in the code, for example:

      $mymetadata1 = "replace this with your DB metadata";
      $metadata[ __( 'Meta Data 1', 'woocommerce-gateway-stripe' ) ] = sanitize_text_field( $mymetadata1 );

  8. Hi Wil,

    Adding line items as meta data is super useful. I was wondering if you know whether one could generate a Stripe Invoice for every transaction (instead of metadata)?

    1. Hi Tia

      Perhaps you are getting terminology mixed up. A transaction is a single purchase and that’s what the WooCommerce Stripe Payment Gateway plugin does.

      The code I have here adds transaction meta data and line items to the single transaction.

      It’s not possible to bend the Stripe Payment Gateway plugin to create a transaction for every line item. WooCommerce doesn’t work that way.

      Thanks,
      Wil.

  9. Hi Wil,

    Thanks a lot,

    How can I create customer metadata instead of payment metadata?

    Thanks,

    Rodolphe

    1. Hi Rodolphe

      You can add any data you can extract from WordPress or WooCommerce in this code snippet.

      See the second code snippet for additional order and checkout data fields and look at https://woocommerce.github.io/code-reference/classes/WC-Order.html#property_data

      If you’re looking to get user meta data, you will need to make sure the customer is registering and account, then grab the user ID from the order data, then use get_user_meta() to get the data – see https://developer.wordpress.org/reference/functions/get_user_meta/

      Hope that helps,
      Wil.

  10. Hi Wil,

    Awesome article. Quick question on the function.php code. We have all our clients in Stripe, but we want to import their data from Stripe to WooCommerce Subscriptions. We’ve been able to import the users, orders, and subscriptions along with the Customer ID (cus_), but we can not seem to generate / import the Stripe Source ID (src_).

    Will adding “The Code” you referenced (linked below) help generate or import the Source ID, so we can add to the subscription billing information? Basically, we want the last 4 digits of the card to show up in the WooCommerce “My Account” > “Payment Methods” screen. Currently, it’s only showing under the Subscriptions screen when we add the Card ID (card_) to the Source ID (src_) area.

    https://zeropointdevelopment.com/wp/how-to-add-custom-metadata-to-woocommerce-stripe-purchase/#:~:text=WooCommerce%20Payments%20plugin.-,The%20Code,You%20can%20find%20the%20full%20code%20at%3A,-/**

    1. Hi Billy

      No, WooCommerce has no access to the Payment Card number – that’s within Stripe.

      You will need to write some custom code to perform an API function request to Stripe to get the card data, probably hooking it into the woocommerce_order_status_completed hook.

      You can’t get full access to the card number, but if you have access to the source object then it should return the last 4 digits in the JSON. You can do that by passing the customer_id.

      Here’s the JS way of doing that.
      var customerService = new StripeCustomerService();
      StripeCustomer customer = customerService.Get("cus_BwS21a7fAH22uk");

      You’ll get something similar to this back:
      JSON: {
      "object": "list",
      "data": [
      {"id":"card_1BY8Hy58hTmWTcG3uosK2up","object":"card","address_city":null,"address_country":null,"address_line1":null,"address_line1_check":null,"address_line2":null,"address_state":null,"address_zip":null,"address_zip_check":null,"brand":"Visa","country":"AU","customer":"cus_JKYFJYUFDap2JQ","cvc_check":"pass","dynamic_last4":null,"exp_month":12,"exp_year":2017,"fingerprint":"yEslkdf4GcxQ","funding":"unknown","last4":"6666","metadata":{},"name":null,"tokenization_method":null}
      ],
      "has_more": false,
      "total_count": 1,
      "url": "/v1/customers/cus_BvylB8PAVap2JQ/sources"
      }

      Note “last4″:”6666” in the data object for the test card.

Comments are closed.