I am trying to add 2 customer attribute fields in my Magento registration form.
The input names are the same in the register.phtml as in the install-1.0.0.php file. However I think there is something wrong in the installer file, because only the first attribute is saved (is_privacy_statement).
Here is my code from the installer file:
$installer = $this;
$installer->startSetup();
$installer->addAttribute("customer", "is_privacy_statement", array(
"type" => "int",
"label" => "Privacy Statement",
"input" => "checkbox",
"position" => 120,
"required" => true,
"is_system" => 0
));
$installer->addAttribute("customer", "is_general_terms", array(
"type" => "int",
"label" => "General Terms",
"input" => "checkbox",
"position" => 121,
"required" => true,
"is_system" => 0
));
$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "is_privacy_statement", "is_general_terms");
$used_in_forms=array();
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_user_defined", 1)
->setData("is_visible", 1);
$attribute->save();
$installer->endSetup();
I have no idea why is not saved the second attribute too.
3 Answers 3
Try this code :
$attributeArray = [
[
'code' => 'is_privacy_statement',
'label' => 'Privacy Statement',
'position' => '1020'
],[
'code' => 'is_general_terms',
'label' => 'General Terms',
'position' => '1021'
]
];
foreach($attributeArray as $attributeCreate)
{
$installer = new Mage_Customer_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->addAttribute(
"customer", $attributeCreate['code'], array(
"type" => "int",
"label" => $attributeCreate['label'],
"input" => "text",
"visible" => true,
"required" => true,
"default" => "1",
)
);
$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", $attributeCreate['code']);
$used_in_forms = array();
$used_in_forms[] = "adminhtml_customer";
$used_in_forms[] = "checkout_register";
$used_in_forms[] = "customer_account_create";
$used_in_forms[] = "customer_account_edit";
$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", $attributeCreate['position']);
$attribute->save();
$installer->endSetup();
}
Note: input type are changed, as per "On customer register got an error message: "Cannot save the customer"" because got errors with checkbox and/or boolean type, and the code a little bit optimised.
-
Thank you. For some reason I thought this will work, it seemed to make sense, but it is still saving the first attribute only.user65808– user658082018年06月08日 12:10:57 +00:00Commented Jun 8, 2018 at 12:10
So your code should be like this after changes:
$installer = $this;
$installer->startSetup();
$installer->addAttribute("customer", "is_privacy_statement", array(
"type" => "int",
"label" => "Privacy Statement",
"input" => "checkbox",
"position" => 120,
"required" => true,
"is_system" => 0
));
$installer->addAttribute("customer", "is_general_terms", array(
"type" => "int",
"label" => "General Terms",
"input" => "checkbox",
"position" => 121,
"required" => true,
"is_system" => 0
));
$used_in_forms=array();
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$attributeIsPrivacyStatement = Mage::getSingleton("eav/config")->getAttribute("customer", "is_privacy_statement");
$attributeIsPrivacyStatement->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_user_defined", 1)
->setData("is_visible", 1);
$attributeIsPrivacyStatement->save();
$attributeIsGeneralTerms = Mage::getSingleton("eav/config")->getAttribute("customer", "is_general_terms");
$attributeIsGeneralTerms->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_user_defined", 1)
->setData("is_visible", 1);
$attributeIsGeneralTerms->save();
$installer->endSetup();
-
Thank you. It is not working. I have double checked the names and deleted the cache. Still the first attribute only is saved.user65808– user658082018年06月08日 12:20:34 +00:00Commented Jun 8, 2018 at 12:20
-
check that
is_general_termsattribute is exist in database.kunj– kunj2018年06月08日 12:24:02 +00:00Commented Jun 8, 2018 at 12:24 -
It does exist only the
is_privacy_statementattribute.user65808– user658082018年06月08日 12:35:57 +00:00Commented Jun 8, 2018 at 12:35 -
remove your module from
core_resourceand remove the first attribute and install again OR try withupgrade-1.0.0-1.0.1.phpadd with the second attribute.kunj– kunj2018年06月08日 12:43:13 +00:00Commented Jun 8, 2018 at 12:43 -
I have removed the data as per your comment and now "Cannot save the customer."user65808– user658082018年06月08日 12:58:04 +00:00Commented Jun 8, 2018 at 12:58
here is my code for create account in add customer custom field and using with patch/data so you can add multiple customer custom attribute with multiple file so you can easily handle from there.
I've created for business name like this
setup/patch/data
<?php namespace Praas\CustomerAccountRegistration\Setup\Patch\Data; use Magento\Catalog\Ui\DataProvider\Product\ProductCollectionFactory; use Magento\Customer\Model\Customer; use Magento\Customer\Model\ResourceModel\Attribute; use Magento\Eav\Model\Config; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Exception\AlreadyExistsException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\Patch\PatchRevertableInterface; use Psr\Log\LoggerInterface; class AddBusinessNameAttribute implements DataPatchInterface, PatchRevertableInterface { /** * @var ModuleDataSetupInterface */ private $moduleDataSetup; /** * @var EavSetupFactory */ private $eavSetupFactory; /** * @var ProductCollectionFactory */ private $productCollectionFactory; /** * @var LoggerInterface */ private $logger; /** * @var Config */ private $eavConfig; /** * @var Attribute */ private $attributeResource; /** * AddPhoneAttribute constructor. * @param EavSetupFactory $eavSetupFactory * @param Config $eavConfig * @param LoggerInterface $logger * @param Attribute $attributeResource */ public function __construct( EavSetupFactory $eavSetupFactory, Config $eavConfig, LoggerInterface $logger, Attribute $attributeResource, ModuleDataSetupInterface $moduleDataSetup ) { $this->eavSetupFactory = $eavSetupFactory; $this->eavConfig = $eavConfig; $this->logger = $logger; $this->attributeResource = $attributeResource; $this->moduleDataSetup = $moduleDataSetup; } /** * {@inheritdoc} */ public function apply() { $this->moduleDataSetup->getConnection()->startSetup(); $this->addPhoneAttribute(); $this->moduleDataSetup->getConnection()->endSetup(); } /** * @throws AlreadyExistsException * @throws LocalizedException * @throws \Zend_Validate_Exception */ public function addPhoneAttribute() { $eavSetup = $this->eavSetupFactory->create(); $eavSetup->addAttribute( Customer:: ENTITY, 'business_name', [ 'type' => 'varchar', 'label' => 'Business Name', 'input' => 'text', 'required' => 0, 'visible' => 1, 'user_defined' => 1, 'sort_order' => 999, 'position' => 999, 'system' => 0 ] ); $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY); $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY); $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'business_name'); $attribute->setData('attribute_set_id', $attributeSetId); $attribute->setData('attribute_group_id', $attributeGroupId); $attribute->setData('used_in_forms', [ 'adminhtml_customer', 'adminhtml_checkout', 'customer_account_create', 'customer_account_edit' ]); $this->attributeResource->save($attribute); } /** * {@inheritdoc} */ public static function getDependencies() { return []; } public function revert() { } /** * {@inheritdoc} */ public function getAliases() { return []; } }
view/frontend/templates in additional.phtml
<div class="field business_name"> <label class="label" for="businessname"> <span><?= $block->escapeHtml(__('Business Name')) ?></span> </label> <div class="control"> <input type="text" name="business_name" id="business_name" value="" title="<?= $block->escapeHtmlAttr(__('Business Name')) ?>" class="input-text"> </div>customer account file in layout customer_account_create.xml
-
<?xml version="1.0"?> <page xmlns:xsi="w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="form.additional.info"> <block class="Magento\Framework\View\Element\Template" name="form_additional_info_customer" template="Praas_CustomerAccountRegistration::additional.phtml"/> </referenceContainer> </body> </page>Pranav vyas– Pranav vyas2023年04月20日 20:37:59 +00:00Commented Apr 20, 2023 at 20:37
$attribute1 = Mage::getSingleton("eav/config")->getAttribute("customer", "is_privacy_statement"); $attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "is_general_terms");because thatgetAttributemethod have 2 param only.