Skip to content
Advertisement

Woocommerce how to add a pre-defined value to a counter on product page

I am using a click counter on my woocommerce product page where the counter adds the number of clicks on a specific button.

The counter works fine and counts the clicks perfectly but I want to add random pre-defined values from given range so each product has unique pre-defined values on counter which is incremented when the button is pressed.

Here are my codes:

Front end (added in through hooks PHP File):

JavaScript

Functions.php:

JavaScript

The code works fine and counts each click on button but, I simply want to add specific pre-defined values which are showed on random bases on each product.

E.g. the pre-defined range is: 5000 – 20000

For example:

  • Second product counter value would be 5523.
  • Third product counter value would be 15789

one button on product a should have:

The value should add one if the corresponding button is pressed once

I am still learning PHP and WordPress so any help would be appreciated.

Advertisement

Answer

In the update_counter_ajax() function, when you define a default value for your counter if it’s empty, you could generate a random number of your choice instead of setting to “1”!

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement