1

Before continuing I would like to state that I have been looking for over a week through StackOverFlow, StackExchange, Google, and Alan Storm blogs.

Please don't link to another post I've most likely read it, instead just view my code and compare it to whatever you want to link and tell me why their way works and mine doesn't.

QUESTION: I want to add an attribute to all customers either an Object that has 3 Keys and 3 Values OR 3 separate values. Here is my Setup so far.

app/code/local/Sean/CustomerAddCustomAttr/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Sean_CustomerAddCustomAttr>
            <version>0.1.1</version>
        </Sean_CustomerAddCustomAttr>
    </modules>
    <global>
        <resources>
            <CustomerAddCustomAttr_setup>
                <setup>
                    <module>Sean_CustomerAddCustomAttr</module>
                    <class>Sean_CustomerAddCustomAttr_Model_Resource_Eav_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </CustomerAddCustomAttr_setup>
            <CustomerAddCustomAttr_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </CustomerAddCustomAttr_write>
            <CustomerAddCustomAttr_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </CustomerAddCustomAttr_read>
        </resources>
    </global>
</config>

app/code/local/Sean/CustomerAddCustomAttr/Model/Resource/Eav/Mysql4/Setup.php

<?php
class Sean_CustomerAddCustomAttr_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup {

}

app/code/local/Sean/CustomerAddCustomAttr/sql/CustomerAddCustomAttr_setup/mysql4-install-0.1.0.php

<?php
$installer = $this;
$installer->startSetup();

$setup = Mage::getModel('customer/entity_setup', 'core_setup');

$setup->addAttribute('customer', 'silver', array(
    'type' => 'int',
    'input' => 'int',
    'label' => 'Silver Membership',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 0,
    'default' => "",
    'visible_on_front' => 0,
    'source' => NULL,
));

$setup->addAttribute('customer', 'gold', array(
    'type' => 'int',
    'input' => 'int',
    'label' => 'Gold Membership',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 0,
    'default' => "",
    'visible_on_front' => 0,
    'source' => NULL,
));

$setup->addAttribute('customer', 'platinum', array(
    'type' => 'int',
    'input' => 'int',
    'label' => 'Platinum Membership',
    'global' => 1,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 0,
    'default' => "",
    'visible_on_front' => 0,
    'source' => NULL,
));

$installer->endSetup();

I've tested and it gets to my installation page because I tried die("We made it to the installation!) at the top and that showed.

However when I run

$customer = Mage::getSingleton('customer/customer'); var_dump($customer); The current customer and all other customers don't have the Silver Gold and Platinum attributes :/

I can see that the customer doesn't have any new attributes :( Can anyone explain why? Or see an error in my code? I've tried deleting the CustomerAddCustomAttr_setup table and refreshing/clearing the Magento Cache but no cigar :/

I have read the following: http://alanstorm.com/magento_models_orm http://alanstorm.com/magento_controller_hello_world http://excellencemagentoblog.com/blog/2011/10/02/customer-registration-fields-magento1-6/ https://stackoverflow.com/questions/26732761/magento-create-new-customer-attribute Adding custom attribute to Customer http://excellencemagentoblog.com/blog/2011/10/02/customer-registration-fields-magento1-6/ http://www.magecorner.com/adding-custom-customer-attributes/

  • Can you elaborate, what you mean with "add an object/associative array as an attribute"? Your code adds the attribute as a text field. What do you want to do with it? – Fabian Schmengler Jun 8 '16 at 20:05
  • For now I would be content settling with just adding a single text field. However even that is not working... In the very end though I would love to have an object. – CodingMageSheen Jun 8 '16 at 20:09
  • Once I activate my module will it automatically run the install scripts? – CodingMageSheen Jun 8 '16 at 20:10
  • if you named it install-0.0.1.php, and did not add it after you added the <customerattribute_setup> node to your config.xml, yes. – Fabian Schmengler Jun 8 '16 at 20:20
  • Then remove the "customerattribute_setup" row from the "core_resource" database table and clean the cache to trigger the script again – Fabian Schmengler Jun 8 '16 at 21:25
1

From the comments:

If you named the script install-0.0.1.php, and did not add it after you added the <customerattribute_setup> node to your config.xml, it should have been executed.

Otherwise, fix that, then remove the "customerattribute_setup" row from the "core_resource" database table and clean the cache to trigger the script again.


After your update:

$customer = Mage::getSingleton('customer/customer'); var_dump($customer);

The current customer and all other customers don't have the Silver Gold and Platinum attributes :/

I think your testing method is flawed. To see an EAV attribute in the object, it first has to have a non-null value. But as long you don't save the customer, no values for the attribute exist (in customer_entity_int).

How to check if an attribute has been created:

  1. look it up in the eav_attributes table:

    SELECT * FROM eav_attribute WHERE attribute_code = 'gold';
    

    -OR-

  2. Test if saving the attribute works, given an existing customer with ID $customerId:

    • save

      $customer = Mage::getModel('customer/customer')->load($customerId);
      $customer->setData('silver', 1);
      $customer->setData('gold', 1);
      $customer->setData('platin', 1);
      $customer->save();
      
    • load

      $customer = Mage::getModel('customer/customer')->load($customerId);
      var_dump(
          $customer->getData('silver'),
          $customer->getData('gold'),
          $customer->getData('platin')
      );
      
  • Thank You. I've literally been spending all week trying to get this to work and you helped in so many ways :). Quick question though, instead of having to set the values to something other than NULL is there a way I could just preset them all to "0" in the install script? – CodingMageSheen Jun 8 '16 at 22:58
  • Well, there is a "default" field for attributes, but it does not work with EAV models like the customer, at least it does not create values for existing customers. I would do this with an SQL query like insert into customer_entity_int (entity_id,attribute_id,value) select entity_id, 666, 0 from customer_entity. Replace 666 with the id of the attribute that you found in eav_attributes. You can execute the query in your install script with $this->run($query). Warning: I wrote that one from the top of my head, test it first! – Fabian Schmengler Jun 8 '16 at 23:03
  • haha okay well I wanted to stay away from mySQL queries I know the other ways of updating them so no worries :)! What would happen hypothetically if I deleted my Sean Directory and everything in it along with the Sean_CustomerAddCustomAttr.xml module? Would the customers still retain their new attributes? – CodingMageSheen Jun 8 '16 at 23:30

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.