d
Topic
Posts:
5
January 12, 2017

Display a random number on the home screen

Hello; I have a line of text that says;

10k+ bought this 

I have it here in; product-grid-item.liquid

Is there a way I can generate a random number between 1 and 10 for each item instead of just having 10 for every item?

To be honest I'm setting up to look similar to wish.com

Thanks  

i
Replies
Posts:
4
January 12, 2017

From 10 years ago, doubt the reasoning has changed - https://ecommerce.shopify.com/c/shopify-discussion/t/can-i-use-random-numbers-in-liquid-1250

So instead you'll need something like this...

<div><span class="my-totally-made-up-number"></span>k+ bought this</div>

and some script like this (using jQuery)

<script>

$( ".my-totally-made-up-number" ).text(function( index ) {
  return Math.floor(Math.random() * 10) + 1;
});

</script>

Jason Shopify Expert freakdesign.com.au
Posts:
8940
January 12, 2017
g
3
upvotes

Using random like that isn't a good idea in this use case. You're talking about changing a number on the page that talks about items sold. You'd do this to gain trust with the customer, but you're going to fake (aka: lie about) the numbers.

If the customer refreshes the page the random number will change showing a totally different amount sold each time. The keen eyed customer will see this and realise it's all fake. You'll lose way more trust than you'll gain.

I'm never down for methods that are not honest with a customer.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
Posts:
5
January 12, 2017

I totally understand. I've sold thousands of these items using dropshipping and woocommerce but I hear where youre coming from and point taken.

Is there a way to actually display what has been sold?

swguy Shopify Partner www.thatsoftwareguy.com
Posts:
33
January 15, 2017

Have you looked at https://apps.shopify.com/metafields-editor as a way of adding this data, then just displaying it in your template? 

Long time Zen Cart modder, getting started with Shopify.
Jason Shopify Expert freakdesign.com.au
Posts:
8940
January 15, 2017

To display what's actually been sold you can store a starting inventory number and just use math to work out the stock that's the been sold. A metafield (as noted above) is a good way to store that starting number.

For example:

  • You have 1000 items in stock
  • You set a metafield to the starting stock amount - in this case 1000
  • As you sell the product the items in stock decreases
  • Using Liquid math, you can work out how many have been sold by taking the current stock value away from the starting number.

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
swguy Shopify Partner www.thatsoftwareguy.com
Posts:
33
January 16, 2017

Jason, could the OP use a metafield to track number sold (initialized at some large number), and then increment this number as sales were made? 

Long time Zen Cart modder, getting started with Shopify.
Jason Shopify Expert freakdesign.com.au
Posts:
8940
January 16, 2017

@swguy - that's largely what I was talking about above although the metafield won't change. It's up to that store to make sure they track stock so the math works. Another approach would be to use an app to update a value on each sale. There's more involved in that but could be an approach if the stock can't be tracked for whatever reason.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
swguy Shopify Partner www.thatsoftwareguy.com
Posts:
33
January 16, 2017

OK, makes sense -  you're updating the Inventory field anyway when you restock, so you may as well update the #sold field.  Thanks.

Long time Zen Cart modder, getting started with Shopify.
CajunDragon Member
Posts:
5
11 days ago

I used the {% assign random = time_seed | times: 1103515245 | plus: 12345 | divided_by: 65536 | modulo: 32768 | modulo: 10 %} someone posted to do a random for 'We only have RANDOM left in stock!' Working well so far except once I got the number 0.

So you've sold thousands!! Great job. I've had my store up 2 weeks and haven't had ONE FLIPPIN sale except my test sale. Pretty sad about it but goning to keep plugging along. :(

Log in or sign up for an account to reply.

f
Your Reply