Jump to content

Adding a customer to a group


ukvapeclub
 Share

Recommended Posts

Hi guys!

 

Ok, so basically what I'm trying to achieve is a module that will add a customer to a group called "MembersOnly" (ID: 5) when their first order is confirmed. This will grant access to a "Members Only" category page for existing customers.

 

This is my first attempt at making a module, and basically I've been cutting, pasting and adapting code from elsewhere to meet the needs of the module. I think I'm right in assuming I've used the "actionValidateOrder" hook properly.

 

At this point I think I just need to know the code that will add the customer to group ID 5.

 

Here's what I've got so far...

<?php   
if (!defined('_PS_VERSION_'))
  exit;
  
 class MembersOnly extends Module
{
	public function __construct()
  {
    $this->name = 'membersonly';
    $this->tab = 'front_office_features';
    $this->version = '1.0.0';
    $this->author = 'Ctrl-V';
    $this->need_instance = 1;
    $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_); 
    $this->bootstrap = true;
 
    parent::__construct();
 
    $this->displayName = $this->l('Members Only');
    $this->description = $this->l('Add paying customers to new group');
 
    $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
 
    if (!Configuration::get('MEMBERSONLY_NAME'))      
      $this->warning = $this->l('No name provided');
  }
  
  public function install()
{
  if (!parent::install() OR !$this->registerHook('actionValidateOrder'))
    return false;
  return true;
}

public function uninstall()
{
  if (!parent::uninstall())
    return false;
  return true;
}

                $context = Context::getContext();
                $id_lang = $context->cart->id_lang;
                $customer = $context->customer;
                $id_customer = $customer->id;
                $groups = Db::getInstance()->executeS("SELECT " ._DB_PREFIX_. "customer_group.id_group , " ._DB_PREFIX_. "group_lang.name
				FROM " ._DB_PREFIX_. "customer_group
                                LEFT JOIN " ._DB_PREFIX_. "group_lang ON " ._DB_PREFIX_. "group_lang.id_group = " ._DB_PREFIX_. "customer_group.id_group
                                WHERE " ._DB_PREFIX_. "customer_group.id_customer = '$id_customer' AND " ._DB_PREFIX_. "group_lang.id_lang = '$id_lang'");
                if(!isset($groups[0])) $groups = FALSE;
}

Now this should return the array $groups containing all groups that the current customer belongs to, I should be able to figure out how to query the array to check if group 5 is already present, I just need to know how to add group 5 if not. I'm assuming it's some kind of SQL wizardry.

 

Hope someone can help me out, and thanks in advance :)

Share this post


Link to post
Share on other sites

Try replacing lines 42-50 with following code:

    public function hookActionValidateOrder($params)
    {
        $this->context->customer->addGroups(array(5));
    }

This should add the customer to group 5 after an order is completed. It appears the Customer::addGroups function uses INSERT IGNORE in its query, so it shouldn't matter if the customer is already in the group.

Share this post


Link to post
Share on other sites

  • 5 years later...

Hi !

I'm trying to do the same thing, when the user finish his subscription to our website, but my module seams to do nothing at all !

Here my code :

<?php

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

class AttachToClientGroup extends Module
{
    public function __construct()
        {
            $this->name = 'attachtoclientgroup';
            $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('Attach 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->update();
    //$this->context->customer->addGroups(3);
    print_r("print_r TEST");
    
    
}

?>

 

the print_r does not display the test message 😕

This is pretty simple did i miss something ?

 

Thanks in advance !

edit : prestashop 1.7.6.5

Edited by nguigno (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