1

I'm currently running Magento ver. 1.7.0.2

I want to add a custom attribute called memberDays to all customers. The catch is I want it to be an associative array or object.

1.)What is the best practice to do this? (My attempted code is below)
2.)Where should this go so that it is not on my front-end. If you could explain why also, it would be much appreciated I love learning new stuff everyday ^_^

Ex. (What I want customers to have as an attribute)

memberDays = {
    Silver => 0,
    Gold => 0,
    Platinum => 0
}

I've only been successful in changing attributes for both Products and Customers as well as adding Product Attributes in the backend Admin Panel. Unfortunately you can't add customer attributes in the Admin panel so I'm here requesting your help!

$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$setup->addAttribute('customer', 'member_days', array(
    'input'         => 'text',
    'type'          => 'string',
    'label'         => 'Days Remaining for Membership',
    'visible'       => 1,
    'required'      => 0,
    'user_defined' => 0,
    'membership_array' => (Silver => 0, Gold => 0, Platinum => 0)
));

$setup->addAttributeToGroup(
 $entityTypeId,
 $attributeSetId,
 $attributeGroupId,
 'member_days',
 '999'  //Place this as Last Attribute
);

$setup->endSetup();
4
  • So you should create the attribute now, and you want to set a value for it when a customer adds product 9567 to their cart? What do you want to set the value to?
    – scrowler
    Commented Jun 2, 2016 at 23:59
  • Sure i'll clarify the question a bit more Commented Jun 3, 2016 at 0:16
  • Customers and products are both EAV, and both have the same hierarchy - you shouldn't have any trouble adding a customer attribute. You may want to evaluate the overall plan though, because this kind of thing seems like a subscription date type thing and you might find it better to set a "gold expiry" attribute to 365 days from when created so you can track when it expires, rather than removing a day every day for every customer that has a value...
    – scrowler
    Commented Jun 3, 2016 at 1:10
  • Thanks Robbie, the difference is that in the Admin Panel you can add attributes to products but for customers you cannot. My question is asking since that is the case what would be the best practice to adding a attribute for customers programatically? Commented Jun 3, 2016 at 1:26

1 Answer 1

0

Instead of remaining days I recommend to add a timestamp, then you don't need to update this on and on again.

And you can add it as every other attribute and set as backend model:

Mage_Eav_Model_Entity_Attribute_Backend_Array
Mage_Eav_Model_Entity_Attribute_Backend_Serialized

the first one only explodes an array, so your keys get lost, the second serializes, which might be a security problem if you are serializing objects, so be careful with this.

After this you can just write an array into the attribute as with every else

$customer->setMyAttribute(['gold' => now(), 'silver' => mktime(1,1,1)]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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