Jump to content

[solved] replace default group / put customer into a group during registration


vermich

Recommended Posts

Hi everybody,

 

I need your help for the last step of my development.

 

On my shop each customer have to be in a Company commits.

 

Each groups (named company commits) is associate to a code and an adress on table ps_group_lang.

 

Currently, customer write the code during registration, it is associate to the customer in ps_customer and i have to compare customer code and group code in backoffice and put the customer into the right Company commits.

 

I would like to automate this step but i don't know where i can replace default group by the company's id_group

 

$codece = Tools::getValue('codece'); // code compagny from registration
	$sql = "SELECT id_group FROM ps_group_lang WHERE code = '$codece'"; //
	$id_default_group = (int)Db::getInstance()->getValue($sql);

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

The correct way would be to create a module that hooks createaccount. You can then add your code to that function.

 

something like the below (untested)

 

public function hookCreateAccount($params)
{
//this is the customer object
$customer = $params['newCustomer'];

$codece = Tools::getValue('codece'); // code compagny from registration
$sql = "SELECT id_group FROM ps_group_lang WHERE code = '$codece'"; //
$id_default_group = (int)Db::getInstance()->getValue($sql);

$groupsToAdd=array();
array_push($groupsToAdd,$id_default_group);
$customer->addGroups($groupsToAdd);
}

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

thanks for your time,

 

i try this yesterday but nothing happend. customer is in the default group.

 

my module :

 

<?php
if (!defined('_PS_VERSION_'))
 exit;

 class LierCustomerCe extends Module
{
public function __construct()
  {
 $this->name = 'liercustomerce';
 $this->tab = 'administration';
 $this->version = '1.0';
 $this->author = 'Romain Vermeeren  ';
 $this->need_instance = 0;
 $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');
 $this->dependencies = array('codece');

 parent::__construct();

 $this->displayName = $this->l('Lier customer');
 $this->description = $this->l('Lier le client à son CE');

 $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
  }

public function install()
{
  if (Shop::isFeatureActive())
 Shop::setContext(Shop::CONTEXT_ALL);

  return parent::install() &&
 $this->registerHook('createAccount') &&
 Configuration::updateValue('MYMODULE_NAME', 'my friend');
   }

public function uninstall()
{
  return parent::uninstall() && Configuration::deleteByName('MYMODULE_NAME');
  Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'liercodece`');
}

public function hookCreateAccount($params)
{
    //this is the customer object
    $customer = $params['newCustomer'];
    $codece = Tools::getValue('codece'); // code compagny from registration
    $sql = "SELECT id_group FROM ps_group_lang WHERE code = '$codece'"; //
    $id_default_group = (int)Db::getInstance()->getValue($sql);
    $groupsToAdd=array();
    array_push($groupsToAdd,$id_default_group);
    $customer->addGroups($groupsToAdd);
 echo $codece;
}
}
?>

 

i made a mistake ?

Link to comment
Share on other sites

If I understood you correctly you want the move the customer from default group to a new group

	   //this is the customer object
		$customer = $params['newCustomer']; // Loads customer
		$codece = Tools::getValue('codece'); // Gets the CodeCe
		$sql = "SELECT id_group FROM ps_group_lang WHERE code = '$codece'"; // Get the id from ps_group_lang, but unless you added a custom field in that table, there is no field called code in ps 1.5
		$id_default_group = (int)Db::getInstance()->getValue($sql); // run the sql
		$groupsToAdd=array(); // array is empty
		array_push($groupsToAdd,$id_default_group); // add id to array
		$customer->addGroups($groupsToAdd);

Your problem might be here. As I see it, you don't get any result from the sql.

Link to comment
Share on other sites

thanks for your help!

 

i just see that it's not "publicfunction hookCreateAccount($params)" but "publicfunction hookcreateAccount($params)"

.It works better :blink:

But now the customer is into two groups. Code CE become a new group and does not replace the default group.

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

public function setDefaultGroupId($customer,$id_default_group) {
  Db::getInstance()->update('customer', array(
'id_default_group' => $id_default_group,
  ), '`id_customer` = '.(int)$customer->id_customer);
}

Just add setDefaultGroupId($customer,$id_default_group) after updateGroup.

 

It's completly untested, but I think it should work.

Link to comment
Share on other sites

excuse me, can you explain me what i have to do ?

 

i have this and after add setDefaultGroupid my module can't be load.

 

[...]
public function hookcreateAccount($params)
{
	//this is the customer object
	$customer = $params['newCustomer'];

	$codece = Tools::getValue('codece'); // code compagny from registration
	$sql = "SELECT id_group FROM ps_group_lang WHERE code = '$codece'"; //
	$id_default_group = (int)Db::getInstance()->getValue($sql);
	$groupsToAdd=array();
	array_push($groupsToAdd,$id_default_group);
	$customer->updateGroup($groupsToAdd);

}
[...]

 

Fatal error: Call to undefined function setDefaultGroupId() in /srv/d_charlet/www/www.parking.com/htdocs/backoffice/bac_a_sable/shop/modules/liercustomerce/liercustomerce.php on line 62

 

62. setDefaultGroupId($customer,$id_default_group);

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

public function setDefaultGroupId($customer,$id_default_group) {
  Db::getInstance()->update('customer', array(
	'id_default_group' => $id_default_group,
  ), '`id_customer` = '.(int)$customer->id_customer);
}

public function hookcreateAccount($params)
{
			//this is the customer object
			$customer = $params['newCustomer'];
			$codece = Tools::getValue('codece'); // code compagny from registration
			$sql = "SELECT id_group FROM ps_group_lang WHERE code = '$codece'"; //
			$id_default_group = (int)Db::getInstance()->getValue($sql);
			$groupsToAdd=array();
			array_push($groupsToAdd,$id_default_group);
			$customer->updateGroup($groupsToAdd);
			 $this->setDefaultGroupId($customer,$id_default_group);
}

 

Give this a try.

Forgot to say it should be called as $this->setDefaultGroupId

Link to comment
Share on other sites

New working code.

public function setDefaultGroupId($customer_id,$id_default_group) {
 Db::getInstance()->update('customer', array(
'id_default_group' => $id_default_group,
 ), '`id_customer` = '.(int)$customer_id);
}

 

Changed the call as well

 $this->setDefaultGroupId($customer->id,$id_default_group);

 

 

And some notes on your module.

the uninstall function

 && Configuration::deleteByName('MYMODULE_NAME');
  Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'liercodece`');

Is useless, beacuse as for what I can see, You don't use the Config option, and neither do you add liercodece table, unless the codece module you have set as dependencies uses that.

 

and the install function

&& Configuration::updateValue('MYMODULE_NAME', 'my friend');

Same thing there. You update that value, but nowhere in the module have you created a page to use that configuration value.

I'm guessing leftovers from the tutorial.

  • Like 1
Link to comment
Share on other sites

  • 2 months later...

I'm using PS 1.5.5 and trying to make this work. I updated the hook name to actionCustomerAccountAdd to match the 1.5 naming scheme change. I commented out the codece dependency to get the module to install. I also have a more direct approach with no extra columns in the db, the reg. code simply equals the group name. Do I understand correctly that this should work by just installing the module? Or do I have to add overrides somewhere? I'm a newbie with PS.
 
Here is my form markup in authentication.tpl:

        <p class="text">
            <label for="customergroup">{l s='Group code'}</label>
            <input name="customergroup" type="text" class="text" id="customergroup" />
        </p>

Here is my modified module code:

if (!defined('_PS_VERSION_'))
  exit;
 
  class LierCustomerCe extends Module
{
    public function __construct()
      {
        $this->name = 'liercustomerce';
        $this->tab = 'administration';
        $this->version = '1.0';
        $this->author = 'Romain Vermeeren        ';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');
//        $this->dependencies = array('codece');
    
        parent::__construct();
    
        $this->displayName = $this->l('Lier customer');
        $this->description = $this->l('Lier le client à son CE');
      }
    

    public function setDefaultGroupId($customer_id,$id_default_group) {
  Db::getInstance()->update('customer', array(
    'id_default_group' => $id_default_group,
  ), '`id_customer` = '.(int)$customer_id);
}

public function actionCustomerAccountAdd($params)
{
        //this is the customer object
        $customer = $params['newCustomer'];
        $codece = Tools::getValue('customergroup');
        $sql = "SELECT id_group FROM ps_group_lang WHERE name = '$codece'";
        $id_default_group = (int)Db::getInstance()->getValue($sql);
        $groupsToAdd=array();
        array_push($groupsToAdd,$id_default_group, 3); // 3 is the "Customer" group
        $customer->updateGroup($groupsToAdd);
        $this->setDefaultGroupId($customer->id,$id_default_group);
}
}

I have no other modifications or overrides.

edit: added the group "Customer" to the array_push.

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

I have this in authentification.tpl

 

<p class="required text">
<input type="text" name="codece" id="codece" value="{if isset($smarty.post.codece)}{$smarty.post.codece}{/if}" />
<label for="codece">{l s='Code CE'}<sup>*</sup></label>
</p>

 

 

the module is ok. i think you just have to add this

public $codece;

in classes/Customer.php ( at the beginning).

 

 

I also add this in controller/front/authcontroller.php line 380 

 

$codecemaj = Tools::getValue('codece');
$codece = strtolower($codecemaj);
if (!empty($codece)){
$sql = "SELECT COUNT(*) FROM ps_group_lang WHERE code = '$codece'";
$group_ce = Db::getInstance()->getValue($sql);
if ( $group_ce == 0)
$this->errors[] = Tools::displayError('Ce code CE existe pas.', false);
}

in order to show an error

 

Edited by vermich (see edit history)
  • Like 1
Link to comment
Share on other sites

Thanks for the details. I still can't get it to work, though. I'm wondering, where I should put p($codece) debug code to display the contents of the variable?

edit: strange, I can nevertheless get it to show the error, if I enter a non-existing customer group, so the $codece variable contains the correct POST data..

 

edit2: damn, I had stupidly deleted this install function

    public function install()
    {
      if (Shop::isFeatureActive())
        Shop::setContext(Shop::CONTEXT_ALL);
    
      return parent::install() &&
        $this->registerHook('createAccount');
        }
And apparently my changing of hookcreateAccount to actionCustomerAccountAdd was blocking this from working. But why in the docs it is described as changed to actionCustomerAccountAdd??

edit3: To answer my own question, I should get my eyes checked.. I didn't notice I have to add "hook" so the method is hookactionCustomerAccountAdd.

 

Thanks a million for this vermich (and PhpMadman), it works now!!

 

Btw., I tried to create overrides for Customer.php and AuthController.php, but they messed things up (like creating the customer without any group so the whole shop was a stub).

 

here are the contents of my override files:

class AuthController extends AuthControllerCore
{
    /**
     * Process submit on an account
     */
    protected function processSubmitAccount()
    {
        $codecemaj = Tools::getValue('customergroup');
        $codece = strtolower($codecemaj);
        if (!empty($codece)){
        $sql = "SELECT COUNT(*) FROM ps_group_lang WHERE name = '$codece'";
        $group_ce = Db::getInstance()->getValue($sql);
        if ( $group_ce == 0)
        $this->errors[] = Tools::displayError('Asiakasryhmän koodi on virheellinen.', false);
        }
        parent::processSubmitAccount();
    }
}
class Customer extends CustomerCore
{
    public $codece;
    
}
Final edit: it was just a case of deleting cache/class_index.php to get the overrides to work!! Edited by Beluga (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 2 months later...
  • 1 month later...
  • 7 months later...

Thanks for the details. I still can't get it to work, though. I'm wondering, where I should put p($codece) debug code to display the contents of the variable?

edit: strange, I can nevertheless get it to show the error, if I enter a non-existing customer group, so the $codece variable contains the correct POST data..

 

edit2: damn, I had stupidly deleted this install function

    public function install()
    {
      if (Shop::isFeatureActive())
        Shop::setContext(Shop::CONTEXT_ALL);
    
      return parent::install() &&
        $this->registerHook('createAccount');
        }
And apparently my changing of hookcreateAccount to actionCustomerAccountAdd was blocking this from working. But why in the docs it is described as changed to actionCustomerAccountAdd??

edit3: To answer my own question, I should get my eyes checked.. I didn't notice I have to add "hook" so the method is hookactionCustomerAccountAdd.

 

Thanks a million for this vermich (and PhpMadman), it works now!!

 

Btw., I tried to create overrides for Customer.php and AuthController.php, but they messed things up (like creating the customer without any group so the whole shop was a stub).

 

here are the contents of my override files:

class AuthController extends AuthControllerCore
{
    /**
     * Process submit on an account
     */
    protected function processSubmitAccount()
    {
        $codecemaj = Tools::getValue('customergroup');
        $codece = strtolower($codecemaj);
        if (!empty($codece)){
        $sql = "SELECT COUNT(*) FROM ps_group_lang WHERE name = '$codece'";
        $group_ce = Db::getInstance()->getValue($sql);
        if ( $group_ce == 0)
        $this->errors[] = Tools::displayError('Asiakasryhmän koodi on virheellinen.', false);
        }
        parent::processSubmitAccount();
    }
}
class Customer extends CustomerCore
{
    public $codece;
    
}
Final edit: it was just a case of deleting cache/class_index.php to get the overrides to work!!

 

Hi,

 

Can I use your solutions in the Ps 1.6? Do you create a new module or change some files?

thanks

Angela

Link to comment
Share on other sites

thanks Beluga, 

did you create a specific module ? or which are the files did you change?

 

thanks for your help.

angela

Note: after installing the module, you must transplant the module to the correct hook. Select from the Modules menu the submenu Positions. Click Transplant a module (in the right hand side), select the module and Hook into: actionCustomerAccountAdd and save.

 

Create the file asiakasryhmakoodi.php in the folder modules/asiakasryhmakoodi.

if (!defined('_PS_VERSION_'))
  exit;
 
  class Asiakasryhmakoodi extends Module
{
    public function __construct()
    {
        $this->name = 'asiakasryhmakoodi';
        $this->tab = 'administration';
        $this->version = '1.0';
        $this->author = 'Romain Vermeeren';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');
    
        parent::__construct();
    
        $this->displayName = $this->l('Asiakasryhmäkoodi');
        $this->description = $this->l('Asettaa rekisteröityessä asiakasryhmän automaattisesti koodin perusteella');
    }
    public function install()
    {
      if (Shop::isFeatureActive())
        Shop::setContext(Shop::CONTEXT_ALL);
    
      return parent::install() &&
        $this->registerHook('actionCustomerAccountAdd');
        }    

    public function setDefaultGroupId($customer_id,$id_default_group) {
        Db::getInstance()->update('customer', array(
        'id_default_group' => $id_default_group,
        ), '`id_customer` = '.(int)$customer_id);
    }

    public function hookactionCustomerAccountAdd($params)
    {
            //this is the customer object
            $customer = $params['newCustomer'];
            $codece = Tools::getValue('customergroup');
            $sql = "SELECT id_group FROM "._DB_PREFIX_."group_lang WHERE name = '$codece'";
            $id_default_group = (int)Db::getInstance()->getValue($sql);
            $groupsToAdd=array();
            array_push($groupsToAdd,$id_default_group, 3); // 3 is the "Customer" group
            $customer->updateGroup($groupsToAdd);
            $this->setDefaultGroupId($customer->id,$id_default_group);
    }
}

In the theme's authentication.tpl file, insert the following before {if $newsletter}:

       <p class="text">
            <label for="customergroup">{l s='Asiakasryhmän koodi'}</label>
            <input name="customergroup" type="text" class="text" id="customergroup" value="{if isset($smarty.post.customergroup)}{$smarty.post.customergroup}{/if}" />
        </p>

Create the following override file: override/controllers/front/AuthController.php

 

class AuthController extends AuthControllerCore
{
    /**
     * Process submit on an account
     */
    protected function processSubmitAccount()
    {
        $codecemaj = Tools::getValue('customergroup');
        $codece = strtolower($codecemaj);
        if (!empty($codece)){
            if (substr($codece, 0, 4) != "club") // change according to your system
                $this->errors[] = Tools::displayError('Asiakasryhmän koodi on virheellinen.', false);
            else {
            $sql = "SELECT COUNT(*) FROM "._DB_PREFIX_."group_lang WHERE name = '$codece'";
            $group_ce = Db::getInstance()->getValue($sql);
                if ( $group_ce == 0 )
                $this->errors[] = Tools::displayError('Asiakasryhmän koodi on virheellinen.', false);
                }
        }
        parent::processSubmitAccount();
    }
}
Edited by Beluga (see edit history)
  • Like 1
Link to comment
Share on other sites

 

Note: after installing the module, you must transplant the module to the correct hook. Select from the Modules menu the submenu Positions. Click Transplant a module (in the right hand side), select the module and Hook into: actionCustomerAccountAdd and save.

 

Create the file asiakasryhmakoodi.php in the folder modules/asiakasryhmakoodi.

if (!defined('_PS_VERSION_'))
  exit;
 
  class Asiakasryhmakoodi extends Module
{
    public function __construct()
    {
        $this->name = 'asiakasryhmakoodi';
        $this->tab = 'administration';
        $this->version = '1.0';
        $this->author = 'Romain Vermeeren';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');
    
        parent::__construct();
    
        $this->displayName = $this->l('Asiakasryhmäkoodi');
        $this->description = $this->l('Asettaa rekisteröityessä asiakasryhmän automaattisesti koodin perusteella');
    }
    public function install()
    {
      if (Shop::isFeatureActive())
        Shop::setContext(Shop::CONTEXT_ALL);
    
      return parent::install() &&
        $this->registerHook('actionCustomerAccountAdd');
        }    

    public function setDefaultGroupId($customer_id,$id_default_group) {
        Db::getInstance()->update('customer', array(
        'id_default_group' => $id_default_group,
        ), '`id_customer` = '.(int)$customer_id);
    }

    public function hookactionCustomerAccountAdd($params)
    {
            //this is the customer object
            $customer = $params['newCustomer'];
            $codece = Tools::getValue('customergroup');
            $sql = "SELECT id_group FROM "._DB_PREFIX_."group_lang WHERE name = '$codece'";
            $id_default_group = (int)Db::getInstance()->getValue($sql);
            $groupsToAdd=array();
            array_push($groupsToAdd,$id_default_group, 3); // 3 is the "Customer" group
            $customer->updateGroup($groupsToAdd);
            $this->setDefaultGroupId($customer->id,$id_default_group);
    }
}

In the theme's authentication.tpl file, insert the following before {if $newsletter}:

       <p class="text">
            <label for="customergroup">{l s='Asiakasryhmän koodi'}</label>
            <input name="customergroup" type="text" class="text" id="customergroup" value="{if isset($smarty.post.customergroup)}{$smarty.post.customergroup}{/if}" />
        </p>

Create the following override file: override/controllers/front/AuthController.php

 

class AuthController extends AuthControllerCore
{
    /**
     * Process submit on an account
     */
    protected function processSubmitAccount()
    {
        $codecemaj = Tools::getValue('customergroup');
        $codece = strtolower($codecemaj);
        if (!empty($codece)){
            if (substr($codece, 0, 4) != "club") // change according to your system
                $this->errors[] = Tools::displayError('Asiakasryhmän koodi on virheellinen.', false);
            else {
            $sql = "SELECT COUNT(*) FROM "._DB_PREFIX_."group_lang WHERE name = '$codece'";
            $group_ce = Db::getInstance()->getValue($sql);
                if ( $group_ce == 0 )
                $this->errors[] = Tools::displayError('Asiakasryhmän koodi on virheellinen.', false);
                }
        }
        parent::processSubmitAccount();
    }
}

Many thanks Beluga

 

I changed the module; because i have to test the domain mail of the customer but It works fine!

 

angela

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...