0

I created a module in order to add a group to a customer depending on his profession (I added a field where he can choose between different professions).

<?php

class Solumed_Customer_Model_Customer_Observer extends Mage_Core_Model_Abstract
{
    /*
     * observer for the customer saved event
     */
    public function customer_save_before($observer)
    {
        try {
            $customer = $observer->getCustomer();

            switch ($customer->getProfession()) {

                case 4:
                    $customer->setData('group_id', 4);
                    break;

                case 5:
                    $customer->setData('group_id', 5);
                    break;

                case 6:
                    $customer->setData('group_id', 6);
                    break;

                case 7:
                    $customer->setData('group_id', 7);
                    break;              

                case 8:
                    $customer->setData('group_id', 8);
                    break;

                default:
                    # code...
                    break;
            }
        } catch (Exception $e) {
            Mage::log( "customer_save_before observer failed: " . $e->getMessage() );
        }
    }
}

?>

The problem is that, after creating a new customer, the group doesn't match the chosen profession. For instance, when I create a doctor (group id number 6), the customer group is not "doctor" but something else (nurse).

I tried to see what the code below give when adding a customer so I wrote this code:

$group = Mage::getModel('customer/group')->load(6)->getCustomerGroupCode();

It returns 'doctor' so I don't know where I am wrong.

  • 1) Your switch portion is useless, it'd be quicker to check for the default case and otherwise assign the value to group_id. 2) Have you checked what group_id is assigned to the user in DB? What is the field you added in the DB? – Julien Lachal Oct 27 '15 at 10:30
  • Indeed, it's useless, I changed it to: $customer = $observer->getCustomer(); $customer->setData('group_id', $customer->getProfession()); To answer your second point, I'm new with Magento so I used a module that allowed me to add custom fields to customers. I just saw that the field's visible but not in the system (here is a picture to describe that): puu.sh/kZn1O/007fdcc0cb.png The field is "profession". I don't know if the problem can come from it or not. I checked the group_id in DB and it's not correct. When I add the profession with id 9, it's id 7, with id 8, it's 3 in DB, etc. – Kiritsu Oct 27 '15 at 11:20
  • I forgot to say that I used this module to add fields to customers: magentocommerce.com/magento-connect/… – Kiritsu Oct 27 '15 at 11:23
  • The "Système" column indicates whether or not an attribute is native to Magento, so don't worry about that :) . Please make sure that the <option> ID's do match the attribute value IDs in the DB. Otherwise you'll get mixed results. – Julien Lachal Oct 27 '15 at 13:13
  • How can I check that in the DB? It is quite big and I'm just a beginner in Magento :p – Kiritsu Oct 27 '15 at 13:32
0

I am not sure but it should be like

try {
    $customer = $observer->getCustomer();
    $customerModel = Mage::getModel('customer/customer')->load($customer->getId());
    if($customerModel->getProfession())
    {
       $customerModel->setGroupId($customerModel->getProfession(), $customerModel->getStoreId())->save();
    }
}
catch (Exception $e) {
    Mage::log("customer_save_before observer failed: " . $e->getMessage());
}
  • It returned this fatal error: Fatal error: Maximum function nesting level of '100' reached, aborting! in C:\wamp\www\Website\lib\Varien\Object.php on line 117 – Kiritsu Oct 27 '15 at 11:21
  • How did you try it? can you show it here please? – Akhilesh Patel Oct 27 '15 at 12:04
  • Yes, I did it like that: try { $customer = $observer->getCustomer(); switch ($customer->getProfession()) { default: $customer->setGroupId($customer->getProfession(), $customer->getStoreId())->save(); break; } } catch (Exception $e) { Mage::log( "customer_save_before observer failed: " . $e->getMessage() ); } – Kiritsu Oct 27 '15 at 12:54
  • edited my answer. try it – Akhilesh Patel Oct 27 '15 at 13:01
  • Well, that's strange. This time, there's no error but it gives me the same profession, always. I chose 'nurse', it gives 'General', try 'doctor', same. :/ Maybe the attribute is not added correctly in the DB. I used a module to do that but maybe it isn't good, how can I check if the attribute exists in DB? – Kiritsu Oct 27 '15 at 13:40
0

I found a solution so I am sharing it here, it may not be optimal. Like I said previously, the problem was that, the groups ID were different.

  • In 'eav_attribute_option_value', for a nurse I get 9
  • In 'Customer_group', I get 4

So first I retrieved the value of the attribute corresponding to the id of the drop down list (cf. 9 for a nurse). Then I got a list of all the names of the groups and once I found the nurse, I take the id to, finally, record it in the customer's 'group_id'.

Here's the code (like I said, it's not optimal :p):

try {
            $customer = $observer->getCustomer();
            $profession = $customer->getProfession();
            $groups = Mage::getModel('customer/group');

            $codeProfession = Mage::getModel('eav/entity_attribute_option')
            ->getCollection()
            ->setStoreFilter()
            ->join('attribute', 'attribute.attribute_id=main_table.attribute_id', 'attribute_code')
            ->getItemById($profession)
            ->getData()['value'];

            $group_code = $groups->load($groupName, 'customer_group_code')
                        ->getCollection()
                        ->getData();

            $idProfession = 8;
            for ($i=0; $i < count($group_code); $i++) { 
                if ($group_code[$i]['customer_group_code'] == $codeProfession) {
                    $idProfession = $group_code[$i]['customer_group_id'];
                }
            }

            $customer->setData('group_id', $idProfession);
        }
        catch (Exception $e) {
            Mage::log("customer_save_before observer failed: " . $e->getMessage());
        }

Thanks for the previous help. Problem solved!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.