Jump to content

[Module Creation] Add a customer Group when user subscribe


nguigno
 Share

Recommended Posts

Hi guys,

This is my first module and i'm a bit confuse, actually what i want is pretty simple atleast for a prestashop developer.

My goal is to add my new user automatically in group 3 when he subscribed to our website.

My code does actually nothing i don't know why, could you give me some hints ?

 

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class iModelAttachToClientGroup extends Module
{
    public function __construct()
        {
            $this->name = 'imodelattachtoclientgroup';
            $this->tab = 'others';
            $this->version = '1.0.1';
            $this->author = 'Quentin';
            $this->need_instance = 0;
            $this->ps_versions_compliancy = [
                'min' => '1.6',
                'max' => _PS_VERSION_
            ];
            $this->bootstrap = true;

            parent::__construct();

            $this->displayName = $this->l('iModel Attach user To Client Group');
            $this->description = $this->l('When a customer subscribe to our website, hes attached to the customer group Particuliers TTC, This module add him also into Client group.');

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

            if (!Configuration::get('MYMODULE_NAME')) {
                $this->warning = $this->l('No name provided');
            }
        }   
}


public function install()
{
    return parent::install();
}

public function uninstall()
{
    return parent::uninstall();
}


public function hookcreateAccount($params)
{
    $this->context->customer->addGroups(array(3));
        //$this->context->customer->addGroups(3);

    $this->context->customer->update();

    print_r("print_r TEST");
    ppp("test ppp");
    
    
}

?>

Sorry i'm very lost !

Thanks in advance !!

Best regards.

Share this post


Link to post
Share on other sites

Hi sorry ! i'm using the last version of prestashop 1.7.6.5

So i registered the hook, but i don't understand the "ps native module for exemple" ?

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class iModelAttachToClientGroup extends Module
{
    public function __construct()
        {
            $this->name = 'imodelattachtoclientgroup';
            $this->tab = 'others';
            $this->version = '1.0.1';
            $this->author = 'Quentin';
            $this->need_instance = 0;
            $this->ps_versions_compliancy = [
                'min' => '1.6',
                'max' => _PS_VERSION_
            ];
            $this->bootstrap = true;

            parent::__construct();

            $this->displayName = $this->l('iModel Attach user To Client Group');
            $this->description = $this->l('When a customer subscribe to our website, hes attached to the customer group Particuliers TTC, This module add him also into Client group.');

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

            if (!Configuration::get('MYMODULE_NAME')) {
                $this->warning = $this->l('No name provided');
            }
        } 
    
        public function install()
        {

            return parent::install() and $this->registerHook('hookactionCustomerAccountAdd');
        }

        public function uninstall()
        {
            return parent::uninstall();
        }


        /*public function hookcreateAccount($params)
        {
            $this->context->customer->addGroups(array(3));
                //$this->context->customer->addGroups(3);

            $this->context->customer->update();

            print_r("print_r TEST");
            ppp("test ppp");


        }*/

        public function hookactionCustomerAccountAdd(){
            $this->context->customer->addGroups(array(3));
                //$this->context->customer->addGroups(3);

            $this->context->customer->update();

            print_r("print_r TEST");
            ppp("test ppp");
        }
    
}

 

Edited by nguigno
forgot to paste the code (see edit history)

Share this post


Link to post
Share on other sites

Hi your code somthing look like this 

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

class iModelAttachToClientGroup extends Module
{
    public function __construct()
        {
            $this->name = 'imodelattachtoclientgroup';
            $this->tab = 'others';
            $this->version = '1.0.1';
            $this->author = 'Quentin';
            $this->need_instance = 0;
            $this->ps_versions_compliancy = [
                'min' => '1.6',
                'max' => _PS_VERSION_
            ];
            $this->bootstrap = true;

            parent::__construct();

            $this->displayName = $this->l('iModel Attach user To Client Group');
            $this->description = $this->l('When a customer subscribe to our website, hes attached to the customer group Particuliers TTC, This module add him also into Client group.');

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

            if (!Configuration::get('MYMODULE_NAME')) {
                $this->warning = $this->l('No name provided');
            }
        } 
    
        public function install()
        {

            return parent::install() && $this->registerHook('actionCustomerAccountAdd');
        }

        public function uninstall()
        {
            return parent::uninstall();
        }

        public function hookActionCustomerAccountAdd($params){
			$customer = $params['newCustomer'];
			$customer->addGroups(array(3)); //3 id of customer group
        }
    
}

if add new hook in your module then uninstall and install module again
Thank you
 

Edited by Nishith (see edit history)

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More