Updating Custom Attributes of Magento Customers Programmatically
As a senior developer at Laravel Company, I often encounter customers who want to add custom attributes to their Magento store for various purposes. One common scenario is when you need to update these custom attributes programmatically during checkout or other processes.
Adding a New Custom Attribute to Magento Customer
You can create new customer attributes using the EAV (Entity-Attribute-Value) model that Magento uses for its object structure. Here’s how to add a new stripe_customer_id attribute:
$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);
$installer->addAttribute("customer", "stripe_customer_id", array(
"type" => "varchar",
"backend" => "",
"label" => "Stripe ID",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
));
$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "stripe_customer_id");
...
$installer->endSetup();
This code creates a new attribute named stripe_customer_id in the default customer attribute set and group. You can see this new field in your Magento admin panel under Customers > Attributes:

Updating Custom Attributes During Checkout
During checkout, you might need to update these custom attributes with data from third-party integrations like Stripe. Here’s the correct way to save a customer and set the stripe_customer_id:
$order = $payment->getOrder();
$customer = Mage::getModel('customer/customer')->load($order->getCustomerId());
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
// Ensure you have a Stripe customer ID before setting it
if ($stripeCustomerId) {
$customer->setStripeCustomerId($stripeCustomerId);
}
try {
// The customer must be saved after setting the attribute value
$customer->save();
Mage::log('Saved: '.$customer->getStripeCustomerId());
} catch (Exception $ex) {
Mage::throwException($ex->getMessage());
}
Troubleshooting Attribute Updates
You mentioned that your code prints the message “Saved: 887475748” but doesn’t show up in the admin panel. There are a few things to check:
-
Flush Cache: Ensure you have flushed all Magento cache types, including configuration, layout, block HTML output, and collection data.
-
Check Attribute Configuration: Verify that the
is_systemflag is not set to 1 for your custom attribute in the database tableeav_attribute. If it’s set to 1, Magento won’t save changes to this field. -
Check Customer Save Scope: Make sure you’re saving the customer with the correct website ID using
$customer->setWebsiteId(Mage::app()->getWebsite()->getId()). -
Re-indexing: Re-index your Magento store by going to System > Index Management and reindexing all indexes, especially
reindex_all_indexesor any relevant customer indexes.
Remember that Magento uses EAV models for saving customer data, which have different behaviors compared to flat tables. Always flush the cache after making changes to attributes or customer data.
If you’re still having issues, consider contacting our professional services team at Laravel Company for personalized assistance with your Magento development needs.