Generate a unique number for a product each time its purchased

Generate a unique number for a product each time its purchased

WFM-TTC
Visitor
1 0 0

Hi everyone,

 

I’m looking for a solution or app that does the following:

 

if a certain product is purchased through my store, each time it is purchased I want that product to be assigned a unique number.

 

So if, for example, I had a book for sale, 1000 in stock, each time someone purchased it, it would get its own unique number 

 

can anyone help?

Reply 1 (1)

DaisyVo
Shopify Partner
2598 320 369

Hi @WFM-TTC 

To generate a unique number for a product each time it's purchased, here's a straightforward solution:

Using Shopify Apps

The easiest way is to use an app from the Shopify App Store that offers advanced order customization or product tagging. Here are a couple of apps that might work:

1- Mechanic
Mechanic is a highly customizable automation app for Shopify. You can create a script that assigns a unique number to the product or order. It works by running custom workflows triggered by purchases.


Steps:

  • Install the Mechanic app.
  • Create a task that listens to the order creation event.
  • Write a script to assign a unique identifier to the product in the order.

Example code snippet for Mechanic:

{% assign product_handle = 'your-product-handle' %}

{% for item in order.line_items %}

  {% if item.product.handle == product_handle %}

    {% capture unique_number %}

      {{ item.id | modulo: 10000 }}

    {% endcapture %}

    Assign number: {{ unique_number }}

  {% endif %}

{% endfor %}

 

2- Order Tagger
This app allows you to tag orders with unique information. You can set it up to generate unique numbers and attach them as tags.
Steps:

  • Install Order Tagger.
  • Set up a workflow to apply unique tags for specific products when they are purchased.

Custom Shopify Development

If you prefer a tailored solution, you can add custom code via Shopify’s Admin API. You’ll need a Shopify developer for this method. Here’s how it can work:

1-Set Up a Custom Field
Add a "unique number" metafield to the product.

2-Automate Unique Number Assignment
Use Shopify Webhooks to listen for orders and then trigger a custom script (written in Node.js, Python, etc.) to:

  • Fetch the purchased product.
  • Assign the next unique number from a sequence.
  • Save it to the order or customer notes.

Example Logic in Node.js:

const assignUniqueNumber = async (order) => {

  const productId = order.line_items[0].product_id; // Assuming single product

  const uniqueNumber = generateUniqueNumber(); // Custom logic to generate numbers

  await addTagToOrder(order.id, `Unique-Number-${uniqueNumber}`);

};

My Recommendation

If you're not comfortable with coding or hiring a developer, start with the Mechanic app. It's flexible and doesn't require heavy technical knowledge. For stores with high traffic and bulk orders, consider custom development for better scalability.

If you need more guidance or clarification, feel free to reply, and I'll help you set this up.

Best regards,
Daisy

 

Please let us know if our reply is helpful by giving it a Like or marking it as a Solution!

Avada SEO & Image Optimizer - The #1 SEO solution