News:

Looking for documentation? Take a look on our wiki

Main Menu

Programatically add customer - Invalid Token, while trying to save user

Started by vmfyelloq19, August 30, 2022, 22:47:14 PM

Previous topic - Next topic

vmfyelloq19

Hi friends

I am creating a joomla user programatically, which works just fine. Now I need to create the corresponding virtuemart user also (with the minimum possible required data as these are unkown here yet).
I tried

// [code to create joomla user omitted ]
if( ! class_exists( 'VmConfig' )) require(JPATH_ADMINISTRATOR . '/components/com_virtuemart/helpers/config.php');
VmConfig::loadConfig();
if( ! class_exists('VmModel')) require(JPATH_ADMINISTRATOR . '/components/com_virtuemart/helpers/vmmodel.php');
$vm_user_data = [
  'user_is_vendor' => false
  ,'customer_number' => ''
  ,'virtuemart_shoppergroup_set' => 1
  ,'username' => 'same-as-for-joomla-user'
  ,'first_name' => 'same-as-for-joomla-user'
  ,'last_name' => 'same-as-for-joomla-user'
  ,'company' => ''
  ,'address_1' => ''
  ,'address_2' => ''
  ,'zip' => ''
  ,'city' => ''
  ,'state' => ''
  ,'virtuemart_country_id' => 0 // TODO
  ,'phone_1' => ''
  ,'email' => 'same-as-for-joomla-user'
  ,'address_type' => 'BT'
  ,'sendEmail' => 0
  ,'virtuemart_user_id' => [ '0' => $id_of_just_created_joomla_user ]
  ,'contact_id' => ''
];
$model_vm_user = VmModel::getModel('user');
$model_vm_user->store($vm_user_data);


But I get the error:
QuoteInvalid Token, while trying to save user

Any ideas?

Jumbo!

Set the session token using vRequest::setVar and add the session token to the $vm_user_data array

Example:

$token = JSession::getFormToken();
$vm_user_data[$token] = 1;
vRequest::setVar($token, 1);


Your codes also needed other corrections. Here is the full set of codes:

JLoader::register('VmConfig', JPATH_ADMINISTRATOR . '/components/com_virtuemart/helpers/config.php');

VmConfig::loadConfig();

$vm_user_data = [
  'user_is_vendor' => false,
  'customer_number' => '',
  'virtuemart_shoppergroup_set' => 1,
  'username' => 'same-as-for-joomla-user',
  'first_name' => 'same-as-for-joomla-user',
  'last_name' => 'same-as-for-joomla-user'
  ,'company' => '',
  'address_1' => '',
  'address_2' => '',
  'zip' => '',
  'city' => '',
  'state' => '',
  'virtuemart_country_id' => 0, // TODO
  'phone_1' => '',
  'email' => 'same-as-for-joomla-user',
  'address_type' => 'BT',
  'sendEmail' => 0,
  'contact_id' => ''
];

$token                = JSession::getFormToken();
$vm_user_data[$token] = 1;
$model_vm_user        = VmModel::getModel('user');

vRequest::setVar($token, 1);

$model_vm_user->setId($id_of_just_created_joomla_user);
$model_vm_user->store($vm_user_data);

vmfyelloq19

Hello Jumbo!

Thank you very much!
it sort of works now but some issues remain:
(Note: the Joomla user is already created)

(1)
Storing fails with "Username in use."
From the virtuemart code it looks like store() is, in parts, a copy of Joomla's own user creation code and it is trying to create the Joomla user again.

(2)
I obeserved that $model_vm_user->setId($id_of_just_created_joomla_user) returns 0. From the code I guess it should return the actual user id.
There are ACL checks in setId() that ssems to fail.

(3)
Although I have set sendEmail=0 it sends a mail to the user about the registration which needs to avoided.

Site note 1:
Also, it looks like I still need to have include

JLoader::register('VmConfig', JPATH_ADMINISTRATOR . '/components/com_virtuemart/helpers/config.php');
VmConfig::loadConfig();
JLoader::register('VmModel', JPATH_ADMINISTRATOR . '/components/com_virtuemart/helpers/vmmodel.php');



Site note 2:
In virtuemart's user.php method setId() is flagged deprecated by Max.

Cheers, vmfyelloq19