Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Make a single random product available to all users

Solved

Make a single random product available to all users

austint1012
Tourist
5 0 1

Hello,

 

I am trying to achieve the ability to have only one random product be available on the store at a time. This will be the same product for all users, so I can’t just run a script on the client side or it would randomize every time a user refreshed and would be different for all users.

 

Where could I write this secure code that would randomly select an active product every x hours? Seems fairly easy, but protecting it so the client couldn’t modify it is important.

Accepted Solutions (2)

Made4uo-Ribe
Shopify Partner
10211 2427 3079

This is an accepted solution.

Hi @austint1012 

 

I assuming your are familiar with the coding. To achieve this, I would create a for loop in all your products, then hide and show one product according to your random code. 

 

Please don't forget to Like and Mark Solution to the post that helped you. Thanks!

If this fixed your issue, Likes and Accept as Solution are highly appreciated. Coffee tips fuel my dedication.
Get experienced Shopify developers at affordable rates—visit Made4Uo.com for a quick quote!
Need THEME UPDATES but have custom codes? No worries, for an affordable price.

View solution in original post

Made4uo-Ribe
Shopify Partner
10211 2427 3079

This is an accepted solution.

Hi @austint1012 

 

It seems like you do not really need randomization. Why dont you just set a product and change it whenever using Theme customization or Shopify Flow

If this fixed your issue, Likes and Accept as Solution are highly appreciated. Coffee tips fuel my dedication.
Get experienced Shopify developers at affordable rates—visit Made4Uo.com for a quick quote!
Need THEME UPDATES but have custom codes? No worries, for an affordable price.

View solution in original post

Replies 7 (7)

Made4uo-Ribe
Shopify Partner
10211 2427 3079

This is an accepted solution.

Hi @austint1012 

 

I assuming your are familiar with the coding. To achieve this, I would create a for loop in all your products, then hide and show one product according to your random code. 

 

Please don't forget to Like and Mark Solution to the post that helped you. Thanks!

If this fixed your issue, Likes and Accept as Solution are highly appreciated. Coffee tips fuel my dedication.
Get experienced Shopify developers at affordable rates—visit Made4Uo.com for a quick quote!
Need THEME UPDATES but have custom codes? No worries, for an affordable price.
austint1012
Tourist
5 0 1
Hello,

Yes I am familiar with HTML, CSS, JS, and a little familiar with Liquid.

If I were to do that in a script on the theme for example, it would be a
different random product for every user - correct? Or is there another
place I can put that code to sync for all users?
austint1012
Tourist
5 0 1

@Made4uo-Ribe 

Hello and thanks for the answer!
 
Yes I am familiar with HTML, CSS, JS, and a little familiar with Liquid.
 
However, if I were to do the randomizing in a script on the theme for example, it would be a different random product for every user - correct?
 
Or is there another place I can put that code to sync for all users? Preferably I would also like to talk to the client to also have a countdown to when the next random product will be selected.
Made4uo-Ribe
Shopify Partner
10211 2427 3079

You can use the date for your random code, so the date would be the same for all user

If this fixed your issue, Likes and Accept as Solution are highly appreciated. Coffee tips fuel my dedication.
Get experienced Shopify developers at affordable rates—visit Made4Uo.com for a quick quote!
Need THEME UPDATES but have custom codes? No worries, for an affordable price.
austint1012
Tourist
5 0 1

Wouldn’t that still leave issues if someone were able to manipulate the browser/devices time zone?

 

Also, how would I keep track of it if I wanted to grab multiple random products in a day? There’s no way to keep them in sync across all clients.

 

It seems like the best route would be creating my own backend server that has an endpoint that returns the active product and the Shopify theme can hit that endpoint via a script. That seems like a lot of work, wondering if Shopify has something more out of the box.

 

Thanks again!

Made4uo-Ribe
Shopify Partner
10211 2427 3079

This is an accepted solution.

Hi @austint1012 

 

It seems like you do not really need randomization. Why dont you just set a product and change it whenever using Theme customization or Shopify Flow

If this fixed your issue, Likes and Accept as Solution are highly appreciated. Coffee tips fuel my dedication.
Get experienced Shopify developers at affordable rates—visit Made4Uo.com for a quick quote!
Need THEME UPDATES but have custom codes? No worries, for an affordable price.
austint1012
Tourist
5 0 1

Hello,

 

That seems like a better solution. Where would I “set” the product as active though? Would that be in Shopify admin or via a custom script in my theme?