2

I added a new custom attribute for Customer using below code.

$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");
$setup->addAttributeToGroup(
        $entityTypeId, $attributeSetId, $attributeGroupId, 'stripe_customer_id', '999'  //sort_order
);
$used_in_forms = array();
$used_in_forms[] = "adminhtml_customer";
$attribute->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100)
;
$attribute->save();
$installer->endSetup();

I can see the new attribute field in Magento admin.

Custom Customer attribute

Now when I try to update the custom attribute on checkout using below code

$order = $payment->getOrder();
$customer = Mage::getModel('customer/customer')->load($order->getCustomerId());
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->setStripeCustomerId('887475748');
try {
    $customer->save();
    print('Saved: '.$customer->getStripeCustomerId());
} catch (Exception $ex) {
   Mage::throwException($ex->getMessage());
}

Above code prints 'Saved: 887475748' and never throws any exception. That means the customer is saving without any error, but when I see the info on admin panel there is nothing in the field.

Note: I refreshed Magento cache several time, flushed cache storage and flushed Magento cache, but no success.

Can someone please help me if I'm missing something here?

1
  • Try deleting the var/cache directory. Sometimes Magento's DDL cache stays behind, and when Magento saves the model, it doesn't "know" about the new attribute. Also, make sure that your customer is a valid customer before saving (check $customer->getId() after loading it). Commented Jan 31, 2015 at 1:14

2 Answers 2

1

You need to add following form entries:

$used_in_forms[]="customer_account_create"; $used_in_forms[]="customer_account_edit";

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use the following code...

$customer->setStripeId('887475748');

This is without knowing the exact name of the variable, but guessing from the text, it looks like you are adding an extra word "customer" in the call to set it.

You can also do this...

$customer['name_of_attribute_here'] = '88747etc';

This way, at times, can be easier to understand than magento's casing and replacement of the _ character in function calls.

What your current code is doing is simply adding a variable to the customer object, not changing part of the customer EAV itself.

1 Comment

His attribute name is "stripe_customer_id" which can be set in 2 ways : 1) $customer->setData('stripe_customer_id','887475748')->save(); OR $customer->setStripeCustomerId('887475748')->save();

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.