Custom field to generate random number

  • Posts: 76
  • Thank you received: 2
1 year 9 months ago #348863

-- HikaShop version -- : 4.5.0
-- Joomla version -- : 4.1.5
-- PHP version -- : 7.4.33

Hi guys,

i need to set up a custom field which would generate random 4 numbers which i'd later show in the invoice.
I'd set it up in the Order table but the question is, is it possible to use the custom text field type to use php code which will generate the random number for me or there's another way to do it?

cheers

Please Log in or Create an account to join the conversation.

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
1 year 9 months ago #348866

Hi,

You can create a mass action with a trigger "after an order is created" with an action "update the values" on the column of your custom field with the mode "operation" and some MySQL commands to generate the number. Several propositions are presented here:
stackoverflow.com/questions/3964624/how-...igit-number-in-mysql
I suppose I would try something like that:

LPAD(FLOOR(RAND()*10000),4,'0')
Otherwise, if you update your HikaShop, you'll have the "run PHP code" action in mass actions, and you can do it with PHP code like this:
$order = new stdClass();
$order->order_id = {order_id};
$order->xxx = rand ( 1000 , 9999 );
$class = hikashop_get('class.order');
$class->save($order);
where xxx is the column of your custom field.

Please Log in or Create an account to join the conversation.

Time to create page: 0.053 seconds
Powered by Kunena Forum