Get Customer Id to personalize the products and orders

Hi, I am working on an app that i want to personalize the products and orders of customers but i am trying to get the customer Id, i found that i can get it using the theme but that would require changes to the current working theme of every store that download my app and i dont think that is practical and i also found that i can inject script tags that would get me the customer Id but it doesn’t work on the orders page. is there another way and did i miss something

Thanks

Hi @Waleed_5

I totally get what you’re trying to do—you want to personalize products and orders for customers, but you’re struggling to fetch the customer ID in a way that works smoothly across all Shopify stores without requiring theme changes. Let’s break this down and explore some practical solutions.

The Issue

You’re right—getting the customer ID through the theme requires modifying store themes, which isn’t ideal since store owners may not want to tweak their designs. Using script tags to inject JavaScript can work on most pages but doesn’t help on the orders page, likely due to Shopify’s restrictions on certain checkout and post-checkout pages.

Best Solutions#### 1. Shopify’s Storefront API (For Frontend Personalization)

If your app runs on the storefront, you can use Shopify’s Storefront API to fetch the customer ID when the user is logged in. Here’s how you can do it using GraphQL:

query {

customer {

id

email

firstName

lastName

}

}

To use this, you’ll need to authenticate using Shopify’s Customer Access Token.

2. Shopify Admin API (For Backend Personalization)

If you need the customer ID for orders, the Admin API is the way to go. When an order is placed, Shopify automatically links it to the customer. You can fetch it using an Admin API request:

GET /admin/api/2024-01/orders.json

{

“orders”: [

{

“id”: 123456789,

“customer”: {

“id”: 987654321,

“email”: “customer@example.com

}

}

]

}

This works well if your app is server-based and needs to personalize orders after they are placed.

3. Shopify App Proxy (For Embedded Apps)

If your app is embedded in the Shopify admin, you can use an App Proxy to securely retrieve the customer ID without modifying themes. This way, your app can request customer details via your backend and return them safely to the frontend.

What You Should Avoid- Relying on Script Tags: Since Shopify blocks certain pages (like the checkout and orders page), this won’t be a fully reliable solution.

  • Modifying Themes: As you already figured out, this isn’t scalable for an app meant for multiple stores.

Best Approach?

If your app is storefront-based, go with the Storefront API. If it needs order data, use the Admin API. And if your app is embedded, an App Proxy might be your best bet.

Hope this helps! If you need extra help, just let me know asap. Thanks
Daisy.