Jump to content

[RESOLU]Groupe différent à l'inscription selon une condition if


pouc

Recommended Posts

Bonjour,

 

Voici mon problème,

 

je souhaiterais lors de l'inscription d'un nouveau client si celui-ci remplie ou non une variable présente sur le formulaire qu'il s'implante dans un groupe différent.

 

Donc dans customer.php j'ai le code suivant:

 

public function __construct($id = null)
    {
        $this->id_default_group = (int)Configuration::get('PS_CUSTOMER_GROUP');
        
        if(empty($mavariable)){
        $this->id_group =6;}
        else{
        $this->id_group =800;}
        
        parent::__construct($id);
        
        
    }

 

Mais ça ne fonctionne pas du tout, quelqu'un aurait-il une solution?

 

Merci.

 

Baptiste

Edited by pouc (see edit history)
Link to comment
Share on other sites

Sous quelle version de Presta êtes-vous ?
 
Ensuite l'approche ne me parait pas la meilleure en tapant dans la classe mère, le controller est fait pour ça
 
Aller dans la fonction protected function processSubmitAccount() et y rajouter vos conditions au niveau de 

						if (!$customer->is_guest)
						{
							$this->context->customer = $customer;
							$customer->cleanGroups();
							// we add the guest customer in the default customer group
							$customer->addGroups(array((int)Configuration::get('PS_CUSTOMER_GROUP')));
							if (!$this->sendConfirmationMail($customer))
								$this->errors[] = Tools::displayError('The email cannot be sent.');
						}
						else
						{
							$customer->cleanGroups();
							// we add the guest customer in the guest customer group
							$customer->addGroups(array((int)Configuration::get('PS_GUEST_GROUP')));
						}

Vous remarquerez que la fonction addGroups() attend un array, qui dans le cas général ne possède qu'une entrée. Vous pouvez donc rajouter la votre. 
Concernant la variable, pas besoin de l'assigner par smarty un simple $ma_variable = Tools::getValue('ma_condition'); suffit.
 
Vous pouvez aussi passer par un module et vous accrocher au hook: actionAuthentication pour effectuer le changement de groupe une fois l'inscription réussie.

  • Like 1
Link to comment
Share on other sites

Merci pour cette réponse mais je manque un peu de connaissance en php et bien que vous m'ayez bien mis sur la piste je ne sais pas absolument pas ou et quoi mettre!!

 

"$ma_variable = Tools::getValue('ma_condition');" je dois le mettre ou exactement? et comment je rédige ma condition?

 

Merci.

 

Baptiste

Link to comment
Share on other sites

Bon, je pense que vous allez modifier le fichier authentification.tpl pour y rajouter votre case à cocher, donc quelquechose du genre

						<div class="checkbox">
							<label for="my_group">
							<input type="checkbox" name="my_group" id="my_group" value="1"  />
							{l s='Votre texte correspondant à la cas à cocher'}</label>
						</div>

Donc vous récupérez la valeur de la checkbox (0 ou 1) dans "$ma_variable = Tools::getValue('my_group');"

Et dans le controller vous remplacez le bloc cité ci-dessus par:

						$ma_variable = Tools::getValue('my_group');
						$id_special_group = 'id du groupe spécial que vous avez créé';
						if(!$customer->is_guest && $ma_variable == 1)
						{
							$this->context->customer = $customer;
							$customer->cleanGroups();
							//Vous incluez votre client dans ce groupe spécial
							$customer->addGroups(array((int)Configuration::get('PS_CUSTOMER_GROUP')), (int)$id_special_group);
							if (!$this->sendConfirmationMail($customer))
								$this->errors[] = Tools::displayError('The email cannot be sent.');						
						}
						elseif (!$customer->is_guest && $ma_variable == 0)
						{
							$this->context->customer = $customer;
							$customer->cleanGroups();
							// we add the guest customer in the default customer group
							$customer->addGroups(array((int)Configuration::get('PS_CUSTOMER_GROUP')));

							if (!$this->sendConfirmationMail($customer))
								$this->errors[] = Tools::displayError('The email cannot be sent.');
						}
						else
						{
							$customer->cleanGroups();
							// we add the guest customer in the guest customer group
							$customer->addGroups(array((int)Configuration::get('PS_GUEST_GROUP')));
						}
Link to comment
Share on other sites

En faite sur mon authentification.tpl j'ai justement ma variable(un numéro d'identification professionnel) qui est déjà présente, et je voudrais faire un contrôle "si cette variable à été remplie" alors le nouvel ,arrivant = groupe par défaut +  autre groupe id=800 sinon  = groupe par défaut + autre groupe id=6."

 

Mais il ne faut pas que le client est à choisir le groupe dans lequel il doit être ça doit être automatique, a moins qu'en suivant votre solution je réalise une checkbox caché sur mon tpl en y mettant la condition si le champ est remplie checkbox =1 sinon = 0.

 

Qu'en pensez-vous?

 

Baptiste

Link to comment
Share on other sites

j'ai rien dans mon tpl mise à part le champ

<p class="text">
            <label for="nthera">{l s='Numéro Thérapeute'}</label>
            <input type="text" class="text" id="nthera" name="nthera" value="{if isset($smarty.post.nthera)}{$smarty.post.nthera}{/if}" />
        </p>

qui est celui que je veux vérifier.

 

Du coup  je suis en train de regarder pour réaliser une  checkbox caché mais c'est plus dur que je ne pensais, et la question que je me pose est ce qu'il faut que crée la checkbox en faisa,t toute les manips de l'ajout dans la bdd de la création en override.

 

Baptiste

Link to comment
Share on other sites

ça ne fonctionne pas!!

 

if (!count($this->errors))
                    {
                        $ma_variable = Tools::getValue('nthera');
                        $id_special_group = '800';
                        
                        if (!$customer->is_guest && $ma_variable == 1)
                        {
                            $this->context->customer = $customer;
                            $customer->cleanGroups();
                            // we add the guest customer in the default customer group
                            $customer->addGroups(array((int)Configuration::get('PS_CUSTOMER_GROUP')),(int)$id_special_group);
                            if (!$this->sendConfirmationMail($customer))
                                $this->errors[] = Tools::displayError('The email cannot be sent.');
                        }
                        elseif (!$customer->is_guest && $ma_variable == 0)
                        {
                            $this->context->customer = $customer;
                            $customer->cleanGroups();
                            // we add the guest customer in the default customer group
                            $customer->addGroups(array((int)Configuration::get('PS_CUSTOMER_GROUP')));

                            if (!$this->sendConfirmationMail($customer))
                                $this->errors[] = Tools::displayError('The email cannot be sent.');
                        }
                        else
                        {
                            $customer->cleanGroups();
                            // we add the guest customer in the guest customer group
                            $customer->addGroups(array((int)Configuration::get('PS_GUEST_GROUP')));
                        }

 

est-ce que j'ai fait une erreur quelques part?

 

Baptiste

Link to comment
Share on other sites

  • 2 weeks later...

Dans mon tpl du coup je n'ai fais aucune modification!!

 

et dans authController :

 

if (!count($this->errors))
                    {
                        $ma_variable = Tools::getValue('nthera');
                        $id_special_group = '800';
                        
                        if (!$customer->is_guest && empty($ma_variable))
                        {
                            $this->context->customer = $customer;
                            $customer->cleanGroups();
                            // we add the guest customer in the default customer group
                            $customer->addGroups(array((int)Configuration::get('PS_CUSTOMER_GROUP')),(int)$id_special_group);
                            if (!$this->sendConfirmationMail($customer))
                                $this->errors[] = Tools::displayError('The email cannot be sent.');
                        }
                        else
                        {
                            $customer->cleanGroups();
                            // we add the guest customer in the guest customer group
                            $customer->addGroups(array((int)Configuration::get('PS_GUEST_GROUP')));
                        }

Edited by pouc (see edit history)
Link to comment
Share on other sites

if (!count($this->errors))
                    {
                        $ma_variable = Tools::getValue('nthera');
                        $id_special_group = '800';
                        
                        if (!$customer->is_guest && !empty($ma_variable))
                        {
                            $this->context->customer = $customer;
                            $customer->cleanGroups();
                            // we add the guest customer in the special customer group
                            $customer->addGroups(array((int)Configuration::get('PS_CUSTOMER_GROUP')),(int)$id_special_group);
                            if (!$this->sendConfirmationMail($customer))
                                $this->errors[] = Tools::displayError('The email cannot be sent.');
                        }
                        elseif (!$customer->is_guest && empty($ma_variable))
                        {
                            $this->context->customer = $customer;
                            $customer->cleanGroups();
                            // we add the guest customer in the default customer group
                            $customer->addGroups(array((int)Configuration::get('PS_CUSTOMER_GROUP')));

                            if (!$this->sendConfirmationMail($customer))
                                $this->errors[] = Tools::displayError('The email cannot be sent.');
                        }
                        else
                        {
                            $customer->cleanGroups();
                            // we add the guest customer in the guest customer group
                            $customer->addGroups(array((int)Configuration::get('PS_GUEST_GROUP')));
                        }

Devrait fonctionner

Link to comment
Share on other sites

Je pense que le votre code doit fonctionner mais par contre j'ai pas l'impression qu'un des codes de presta ne fait pas la différence entre ma variable remplie et ma variable vide.

Donc votre code ne fonctionne pas est-ce qu'il y aurait un moyen de savoir si presta prends en compte ma variable ou pas.

 

Baptiste

Link to comment
Share on other sites

Exact car le client est créé avant d'arriver à cette étape.
Supprimez vos modifs dans le controller et insérez ceci dans la classe Customer.php, ligne 222 juste avant /* Can't create a guest customer, if this feature is disabled */

$ma_variable = Tools::getValue('my_group');
$id_special_group = '4';
if(!empty($ma_variable))
$this->id_default_group = (int)$id_special_group;

 /* Can't create a guest customer, if this feature is disabled */

Changez bien sûr le nom de la variable 'my_group' et l'id du groupe spécial suivant vos paramètres de boutique.
Le code ci-dessus est testé et fonctionnel sur une 1.6 avec cet ajout dans le authentification.tpl juste avant les checkboxes newsletter et optin:

			<div class="checkbox">
				<label for="my_group">
				<input type="checkbox" name="my_group" id="my_group" value="1"  />
				{l s='Votre texte correspondant à la case à cocher'}</label>
			</div>					
			
			{if $newsletter}
...
Link to comment
Share on other sites

Parfait!! j'ai réussi avec quelques légère modification mais en tout cas ça marche.

 

alors ligne 220 dans customer.php j'ai ceux-ci:

 

    public function __construct($id = null)
    {    
        $ma_variable = Tools::getValue('nthera');
        $id_special_group = '800';
    
        if (!empty($ma_variable))
        {
        $this->id_default_group = (int)Configuration::get('PS_CUSTOMER_GROUP');
        $this->id_group = (int)$id_special_group;
        }
        else{
        $this->id_default_group = (int)Configuration::get('PS_CUSTOMER_GROUP');
        $this->id_group =6;
        }
        parent::__construct($id);
    }

 

Donc si ma variable est remplie il est inscrit dans le groupe client et dans l'id 800 sinon il est inscrit dans le groupe client et l'id 6.

 

Merci beaucoup pour votre aide!!

 

Baptiste

Link to comment
Share on other sites

Ce n'est pas une bonne idée de mettre la modif dans le __construct(), car là, à chaque instanciation d'un objet customer vous traitez l'id_default pour rien.

Je vous l'avais mis dans la fonction add(), car c'est celle-ci qui est appelée au moment de l'inscription^^

Link to comment
Share on other sites

  • 3 months later...

Bonjour,

 

Je me permets de relancer le sujet, en ce qui me concerne j'utilise la méthode de surcharge du contrôleur AuthController décrite initialement par Eolia, et cela fonctionne très bien.

Voilà mon code :

if (!count($this->errors))
					{
						if (!$customer->is_guest)
						{
							$this->context->customer = $customer;
							$customer->cleanGroups();
							// we add the guest customer in the default customer group
							$customer->addGroups(array((int)Configuration::get('PS_CUSTOMER_GROUP')));
							if (!$this->sendConfirmationMail($customer))
								$this->errors[] = Tools::displayError('The email cannot be sent.');
						}
						else
						{
							$customer->cleanGroups();
							// we add the guest customer in the guest customer group
							$customer->addGroups(array((int)Configuration::get('PS_GUEST_GROUP')));
						}
            
            /*** 
             * OVERRIDE BEGINS 
             * Auto assignation au groupe pro (id = 4) si SIRET existe
             ***/           
            $siret = (Tools::isSubmit('siret')) ? (int)Tools::getValue('siret') : '';
            if($siret != '') 
            {
                $customer->cleanGroups();
                $customer->addGroups(array((int)4));
            }
...

Mais j'aurais aimé pouvoir rendre ça plus "administrable", en laissant le choix via le backoffice de créer des conditions d'auto-assignation, pour le groupe pro mais également d'autres. Par exemple, en utilisant le champ date de naissance, indiquer une année dans le backoffice qui permettrait de placer un client dans un groupe "jeunes de moins de 25 ans".

Et dans AuthController cela pourrait donner ça :

if($birthyear != '' && (date("Y") - $birthyear <= 25)) 
{   
   $customer->addGroups(array((int)5)); // avec 5 ID du groupe jeunes moins 25 ans
}  

Est-ce qu'un module qui fait ça existe déjà ?

Merci !

Edited by wouababelouba (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...