Jump to content

Module Add Custom Gruop User


Baudo

Recommended Posts

Hi everyone,

I want that when I install my module is added a new group for users.

I try this:

$sql = array();


$sql[] = 'INSERT INTO `'._DB_PREFIX_.'group`
    (reduction,price_display_method,show_prices) VALUES (0.00, 0, 1)';

$idgroup = Db::getInstance()->getValue("SELECT MAX(id) FROM `"._DB_PREFIX_."group`");


foreach ($sql as $query)
	if (Db::getInstance()->execute($query) == false)
		return false;

in $idgroup I have the id of my group, now I have to put the names in the ps_group_lang with the id of the language, is correct?

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

Don't use sql queries when you can use PrestaShop classes

 

 

$languages = Language::getLanguages();
$group_names = array(
'en' => 'new group',
'fr' => 'nouveau groupe' //....
);
$group = new Group();
$group->reduction = 5.00; //%
$group->price_display_method = PS_TAX_EXC; // or PS_TAX_INC
$group->show_prices = false;
$group->name = array();
foreach ($languages as $lang)
{
$id_lang = $lang['id_lang'];
$iso_lang = $lang['iso_code'];
$group->name[$id_lang] = isset($group_names[$iso_lang]) ? $group_names[$iso_lang] : $group_names['en'];
}


$group->save();
Edited by Tuni-Soft (see edit history)
  • Like 1
Link to comment
Share on other sites

Thanks for the suggestion, I have another question, if I want to assign a new user to that category, as do the id of the right category? Work?

function hookCreateAccount($params)
{
    $customer = $params['newCustomer'];
    $idcategory = Configuration::get("MY_MODULE_ID-GROUP")
    $customer->addGroups($idcategory);
}

public function install()
{                
    $languages = Language::getLanguages();
    $group_names = array(
        'en' => 'New Group'
    );
    $group = new Group();
    $group->reduction = 20.00;
    $group->price_display_method = PS_TAX_INC; // or PS_TAX_EXC
    $group->show_prices = true;
    $group->name = array();
    foreach ($languages as $lang){
        $id_lang = $lang['id_lang'];
        $iso_lang = $lang['iso_code'];
        $group->name[$id_lang] = isset($group_names[$iso_lang]) ? $group_names[$iso_lang] : $group_names['en'];
    }
    $group->save();
    Configuration::updateValue('MY_MODULE_ID-GROUP', $group->id); //work after save?
 
    return parent::install() &&  $this->registerHook('createAccount');
}
Edited by Baudo (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...