Plugin OnBeforeUserCreate

  • Posts: 27
  • Thank you received: 0
  • Hikashop Business
10 years 7 months ago #153436

Hello,

I've created a plugin onBeforeUsercreate :

class plgHikashopCode_ent extends JPlugin
{
	function plgHikashopCode_ent(&$subject, $config){
		parent::__construct($subject, $config);
		if(!isset($this->params)){
			$plugin = JPluginHelper::getPlugin('hikashop', 'code_ent');
			if(version_compare(JVERSION,'2.5','<')){
				jimport('joomla.html.parameter');
				$this->params = new JParameter($plugin->params);
			} else {
				$this->params = new JRegistry($plugin->params);
			}
		}
	}

	function onBeforeUserCreate(&$element,&$do) {
		
		$database = JFactory::getDBO();
		$query='SELECT count(*) FROM entreprises WHERE code = "'.$element->code_entreprise.'"';
		$database->setQuery($query);
		$database->query();
		$num_rows = $database->loadResult();
		
		if ($num_rows <= 0)
			$do=false;
		
	}


}
I want to verify a custom information on user before his cart registration.
But it seems that it does'nt launch. My plugin is activated.But when I go to plugin hihashop (System / Plugins), the list is empty.

Where is my mistake ?

Best Regards

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

  • Posts: 13201
  • Thank you received: 2322
10 years 7 months ago #153439

Hi,

The problem come maybe from your XML file.
How do you have made this plugin ? Do you have coded it, and then installed via the Joomla extension manager ?
If not thanks to zip the files and install it via the Joomla installer or use the "discover" function.

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

  • Posts: 27
  • Thank you received: 0
  • Hikashop Business
10 years 7 months ago #153483

Hello,

I've coded this plugin and installed it via the Joomla extension manager.
This s my xml file :

<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" version="2.5" method="upgrade" group="hikashop">
	<name>Hikashop Code_ent Plugin</name>
	<creationDate>{__DAY__} {__MONTH__} {__YEAR__}</creationDate>
	<version>{__PLUGIN_VERSION__}</version>
	<author>*************</author>
	<license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
	<description>Plugin pour gérer le code entreprise</description>
	<files>
		<filename plugin="code_ent">code_ent.php</filename>
	</files>
	<params addpath="/components/com_hikashop/params">
	</params>
</extension>

Best Regards

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

  • Posts: 13201
  • Thank you received: 2322
10 years 6 months ago #153507

The XML seems to be ok, in your request I can see that you select data in the table "enterprises" are you sure that you have a table with that name without prefix ?

In all cases, can you try display system messages at many place in order to see if the plugin is called.

$app = JFactory::getApplication();
$app->enqueueMessage('Here ?');
Put a var_dump on your query result and a "die()" in the class "user" function "save()" after:
$dispatcher->trigger( 'onBeforeUserCreate', array( & $element, & $do) );

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

  • Posts: 27
  • Thank you received: 0
  • Hikashop Business
10 years 6 months ago #153785

Hello,

Yes, the name of my table is : entreprises without extension.
I've display system messages, var_dump on plugin and die in the class user just after $dispatcher->trigger( 'onBeforeUserCreate', array( & $element, & $do) );

I can see system message from plugin on checkout identification.
Then when I launch user creation with the button : a blank page with no message.

The code of my plugin :

function onBeforeUserCreate(&$element,&$do) {
	  

		$database = JFactory::getDBO();
		$query='SELECT count(*) FROM entreprises WHERE code = "'.$element->code_entreprise.'"';
		$database->setQuery($query);
		$database->query();
		$num_rows = $database->loadResult();
		
		//var_dump($query );
		
		if ($num_rows <= 0)
		{
			if ($element->code_entreprise != '')
			{
			$app = JFactory::getApplication();
			$app->enqueueMessage('Votre code entreprise est inconnu');
			}
			$do=false;
		}
		
		
	}

I can see twice 'Votre code entreprise est inconnu' : one with $element->code_entreprise blank and one with $element->code_entreprise fullfilled

If I remove die from user class, the user is created.

I'm lost. Have you got an idea ?

Best Regards

Last edit: 10 years 6 months ago by csim.

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

  • Posts: 13201
  • Thank you received: 2322
10 years 6 months ago #153794

Hi,

The blank page is due to the "die" but if nothing is displayed, that's probably because you have an error.
Have you enabled the debug mode and set the error reporting level to "maximum" in order to see the errors ?

You var_dump should not be empty.

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

  • Posts: 27
  • Thank you received: 0
  • Hikashop Business
10 years 6 months ago #153948

Hello,
I've enabled debug.
So some things become clear but another not.
The name of the plugin in hikashop documentation is not right : it's not OnBeforeUserCreate but onUserBeforeSave.
Moreover the parameters seems not be references.
My code is now like that :

class plgHikashopCode_ent extends JPlugin
{
	function plgHikashopCode_ent(&$subject, $config){
		parent::__construct($subject, $config);
		if(!isset($this->params)){
			$plugin = JPluginHelper::getPlugin('hikashop', 'code_ent');
			if(version_compare(JVERSION,'2.5','<')){
				jimport('joomla.html.parameter');
				$this->params = new JParameter($plugin->params);
			} else {
				$this->params = new JRegistry($plugin->params);
			}
		}
	}

	function onUserBeforeSave($element,$element2,$element3) {
	  
	  	//var_dump($element);
		//var_dump($element2);
		var_dump($element3);

		$database = JFactory::getDBO();
		$query='SELECT count(*) FROM entreprises WHERE code = "'.$element3->code_entreprise.'"';
		$database->setQuery($query);
		$database->query();
		$num_rows = $database->loadResult();
		
		var_dump($num_rows);
		
		$count_rows=(int)$num_rows;
		
		if ($count_rows <= 0)
		{
			if ($element3->code_entreprise != '')
			{
			$app = JFactory::getApplication();
			$app->enqueueMessage('Votre code entreprise est inconnu');
			}
			$do=false;
		}
		
		
	}


}
The plugin is called. But element->code_entreprise not known. That's errors on die showing var_dump($element3) :
array(21) { ["isRoot"]=> NULL ["id"]=> int(0) ["name"]=> string(4) "Test" ["username"]=> string(4) "test" ["email"]=> string(24) "christelle.simon@free.fr" ["password"]=> string(34) "$P$DESPKGG4SCCPVTRjWg3uTqxMK9p83C1" ["password_clear"]=> string(8) "12345678" ["usertype"]=> NULL ["block"]=> NULL ["sendEmail"]=> int(0) ["registerDate"]=> string(19) "2014-04-29 06:37:44" ["lastvisitDate"]=> NULL ["activation"]=> NULL ["params"]=> string(44) "{"site_language":"fr-FR","language":"fr-FR"}" ["groups"]=> array(1) { [2]=> string(1) "2" } ["guest"]=> int(1) ["lastResetTime"]=> NULL ["resetCount"]=> NULL ["aid"]=> int(0) ["password2"]=> string(8) "12345678" ["gid"]=> string(1) "0" } Notice: Trying to get property of non-object in /var/www/vhosts/fair-play.fr/subdomains/cerise/httpdocs/plugins/hikashop/code_ent/code_ent.php on line 34 string(1) "0" Notice: Trying to get property of non-object in /var/www/vhosts/fair-play.fr/subdomains/cerise/httpdocs/plugins/hikashop/code_ent/code_ent.php on line 45

Code entreprise is a custom field. This is a part of identification page :
<tr id="hikashop_user_code_entreprise" class="hikashop_registration_code_entreprise_line">
<td class="key">
<td>
<input id="code_entreprise" class="inputbox" type="text" value="sdfsdf" name="data[user][code_entreprise]" onchange="hikashopToggleFields(this.value,'code_entreprise','user',0);">
<span class="hikashop_field_required">*</span>
</td>
</tr>

Maybe, I'm wrong in calling parameters ?

Best Regards

Last edit: 10 years 6 months ago by csim.

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

  • Posts: 26158
  • Thank you received: 4028
  • MODERATOR
10 years 6 months ago #153975

Hi,

var_dump($element3)
array(21)

First, $element3 is an array, not an object.

More than that, "onUserBeforeSave" is not an HikaShop trigger, but "onBeforeUserCreate" and "onBeforeUserUpdate" are.
www.hikashop.com/support/support/documen...umentation.html#user

You can't access to HikaShop user custom field is you are looking at the Joomla user (using a Joomla user trigger).

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

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

  • Posts: 27
  • Thank you received: 0
  • Hikashop Business
10 years 6 months ago #153999

Hello,

So, i've modifiyed :
My plugin :

class plgHikashopCode_ent extends JPlugin
{
	function plgHikashopCode_ent(&$subject, $config){
		parent::__construct($subject, $config);
		if(!isset($this->params)){
			$plugin = JPluginHelper::getPlugin('hikashop', 'code_ent');
			if(version_compare(JVERSION,'2.5','<')){
				jimport('joomla.html.parameter');
				$this->params = new JParameter($plugin->params);
			} else {
				$this->params = new JRegistry($plugin->params);
			}
		}
	}

	function onBeforeUserCreate(&$element,&$element2) {
	  
	  	var_dump($element);
		var_dump($element2);
		//var_dump($element3);

		$database = JFactory::getDBO();
		$query='SELECT count(*) FROM entreprises WHERE code = "'.$element->code_entreprise.'"';
		$database->setQuery($query);
		$database->query();
		$num_rows = $database->loadResult();
		
		var_dump($num_rows);
		
		$count_rows=(int)$num_rows;
		
		if ($count_rows <= 0)
		{
			if ($element->code_entreprise != '')
			{
			$app = JFactory::getApplication();
			$app->enqueueMessage('Votre code entreprise est inconnu');
			}
			$element2=false;
		}
		
		
	}


}

The user librairy (user.php) :
$dispatcher->trigger( 'onBeforeUserCreate', array( &$element, &$do) );
			die();

The errors now :
NULL NULL Notice: Trying to get property of non-object in /var/www/vhosts/fair-play.fr/subdomains/cerise/httpdocs/plugins/hikashop/code_ent/code_ent.php on line 34 string(1) "0" Notice: Trying to get property of non-object in /var/www/vhosts/fair-play.fr/subdomains/cerise/httpdocs/plugins/hikashop/code_ent/code_ent.php on line 45

Je tourne en rond. Hope there is a solution.
Best Regards

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

  • Posts: 82864
  • Thank you received: 13372
  • MODERATOR
10 years 6 months ago #154011

The plugin code is correct.
If you look in the class/user.php file you can see that code:

$do = true;
		if($new){
			$dispatcher->trigger( 'onBeforeUserCreate', array( & $element, & $do) );
This means that the only possibility for the $do variable to be null in your plugin onBeforeUserCreate function is that another plugin on your website which also implements the onBeforeUserCreate function set that parameter to null before yours.
I don't know why that would happen (?), but a simple workaround is to set a negative ordering to your plugin so that it gets called before all the others. Then, that won't be possible that $do is null in your plugin as there will be virtually no code between the trigger and your plugin.

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

  • Posts: 27
  • Thank you received: 0
  • Hikashop Business
10 years 6 months ago #154059

Hello,

So, i've ordered the plugin code_ent with a negative value (see my first screen image).
But no success : always the error

NULL NULL Notice: Trying to get property of non-object in /var/www/vhosts/fair-play.fr/subdomains/cerise/httpdocs/plugins/hikashop/code_ent/code_ent.php on line 34 string(1) "0" Notice: Trying to get property of non-object in /var/www/vhosts/fair-play.fr/subdomains/cerise/httpdocs/plugins/hikashop/code_ent/code_ent.php on line 45

I wonder if it is normal that the lists of plugin on Hikashop is empty ? (see my second creen image)

Best Regards

Attachments:

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

  • Posts: 13201
  • Thank you received: 2322
10 years 6 months ago #154118

Hi,

Could you give us the corresponding code for the lines "34" and "45" just to be sure of which variable is not an object ?

In HikaShop plugins, you have to click on new in order to enable plugins presents in HikaShop but not enabled by default.
So having nothing in this listing is not a problem.

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

  • Posts: 27
  • Thank you received: 0
  • Hikashop Business
10 years 6 months ago #154143

Hello,

line 34 : $query='SELECT count(*) FROM entreprises WHERE code = "'.$element->code_entreprise.'"';
line 45 : if ($element->code_entreprise != '')

Best Regards

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

  • Posts: 13201
  • Thank you received: 2322
10 years 6 months ago #154264

Hi,

You should use this code to prevent from errors.

class plgHikashopCode_ent extends JPlugin
{
	function plgHikashopCode_ent(&$subject, $config){
		parent::__construct($subject, $config);
		if(!isset($this->params)){
			$plugin = JPluginHelper::getPlugin('hikashop', 'code_ent');
			if(version_compare(JVERSION,'2.5','<')){
				jimport('joomla.html.parameter');
				$this->params = new JParameter($plugin->params);
			} else {
				$this->params = new JRegistry($plugin->params);
			}
		}
	}

	function onBeforeUserCreate(&$element,&$element2) {

		$count_rows = 0;
		if(isset($element->code_entreprise)){
			$database = JFactory::getDBO();
			$query='SELECT count(*) FROM entreprises WHERE code = "'.$element->code_entreprise.'"';
			$database->setQuery($query);
			$database->query();
			$num_rows = $database->loadResult();
			$count_rows=(int)$num_rows;
		}
		
		if ($count_rows <= 0)
		{
			if ($element->code_entreprise != '')
			{
			$app = JFactory::getApplication();
			$app->enqueueMessage('Votre code entreprise est inconnu');
			}
			$element2=false;
		}
	}
}

And are you sure that the Column name of your custom field "Code entreprise" in the user table is "code_entreprise" and not, by default, "codeentreprise" ?

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

  • Posts: 27
  • Thank you received: 0
  • Hikashop Business
10 years 6 months ago #154268

Hello,
I've tried without die and it works.
The plugin is called twice : once without custom fields and another one with them.

Strange :)

The user Joomla is created but it doesn't access to the next level of checkout.
Is it possible not to create user Joomla ?

Best Regards

Attachments:
Last edit: 10 years 6 months ago by csim.

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

  • Posts: 82864
  • Thank you received: 13372
  • MODERATOR
10 years 6 months ago #154314

If you want to plug yourself before the Joomla user account creation, then you need to implement the trigger onBeforeStoreUser of Joomla 'user' plugins. So not a HikaShop plugin but a Joomla user plugin. There, you would have to do a redirect to prevent the code from continuing.

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

  • Posts: 27
  • Thank you received: 0
  • Hikashop Business
10 years 3 days ago #180666

Hello,

Sorry for the delay but I have to face now the problem.
A question if I use onBeforeStoreUser (Joomla Plugin) , will I have access to the entreprise code (a personnalized info for the user that I've added in hikashop => champ personnalisé) ?

Best Regards

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

  • Posts: 82864
  • Thank you received: 13372
  • MODERATOR
10 years 3 days ago #180697

Hi,

Yes, but in that case, you want to look in the $_POST variable for the custom user field data from HikaShop.

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

Time to create page: 0.109 seconds
Powered by Kunena Forum