【问题标题】:custom attribute displaying but is not saving in magento 2 admin customer creation form自定义属性显示但未保存在 magento 2 管理员客户创建表单中
【发布时间】:2017-01-24 11:30:45
【问题描述】:

我在我的 magento 2 环境中创建了新的客户属性

字段已添加,该字段中的数据未保存。我收到错误,因为保存数据时出现问题。我找不到好的教程来添加新属性。请帮助解决这个问题。

我已遵循此代码

https://magento.stackexchange.com/questions/128178/magento-2-add-custom-attribute-in-customer-registration-form

【问题讨论】:

    标签: magento2 custom-attributes


    【解决方案1】:

    客户注册自定义属性值在管理员中创建并保存

    1.文本字段

    2。下拉字段

    3.日期字段

    使用 UpgradeSchema.php

    <?php
    
    namespace {CompanyName}\{ModuleName}\Setup;
    
    use Magento\Customer\Model\Customer;
    use Magento\Customer\Setup\CustomerSetup;
    /* irrelevant */
    #use Magento\Framework\Setup\UpgradeSchemaInterface;
    use Magento\Framework\Setup\InstallSchemaInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    /* irrelevant */
    #use Magento\Framework\Setup\SchemaSetupInterface;
    /* add this */
    use Magento\Framework\Setup\UpgradeDataInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    
    class UpgradeData implements  UpgradeDataInterface
    {
        private $customerSetupFactory;
    
        public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
        {
            $this->customerSetupFactory = $customerSetupFactory;
        }
        public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        {
            if (version_compare($context->getVersion(), '1.0.1', '<')) 
            {
                // For Text field
                $customerSetup->addAttribute(
                        \Magento\Customer\Model\Customer::ENTITY,
                        'attribute_title',                    
                        [
                            'type' => 'text',
                            'input' => 'text',
                            'label' => 'Attribute Title',
                            'required' => false,
                            'visible' => true,
                            'user_defined' => false,
                            'sort_order' => 1000,
                            'position' => 1000,
                            'system' => 0,
                        ]
                    );
                $attribute_title = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'attribute_title')
                    ->addData(
                        ['used_in_forms' => ['adminhtml_customer']
                    ]);
    
                $attribute_title->save();
    
                //Add field Drop Down for Yes/No
                $customerSetup->addAttribute(
                        \Magento\Customer\Model\Customer::ENTITY,
                        'is_attribute',                    
                        [
                            'type' => 'int',
                            'input' => 'select',
                            'label' => 'Is Attribute',
                            'frontend' => '',
                            'default' => '1',
                            'class' => '',
                            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                            'required' => false,
                            'visible' => true,
                            'user_defined' => false,
                            'sort_order' => 1000,
                            'position' => 1000,
                            'system' => 0,
                        ]
                    );
                $is_attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_attribute')
                    ->addData(
                        ['used_in_forms' => ['adminhtml_customer']
                    ]);
    
                $is_attribute->save();
    
                // For Date And Time field
                $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    'custom_date',                    
                    [
                        'label' => 'Custom Date',
                        'type' => 'datetime',
                        'input' => 'date',
                        'frontend' => 'Magento\Eav\Model\Entity\Attribute\Frontend\Datetime',
                        'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\Datetime',
                        'validate_rules' => '{"input_validation":"date"}',
                        'user_defined' => false,
                        'required' => false,
                        'visible' => true,
                        'searchable' => false,
                        'filterable' => false,
                        'comparable' => false,
                        'visible_on_front' => false,
                        'sort_order' => 1000,
                        'position' => 1000,
                        'system' => 0,
                    ]
                );
                $custom_date = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_date')
                    ->addData(
                        ['used_in_forms' => ['adminhtml_customer']
                    ]);
                    // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
                $commenced_business->save();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      数据保存不会自行发生,您需要将数据设置为属性。 如果它是客户界面的一部分 例如

      $attribute = $customer->getCustomAttribute('client_dn');
      if ($attribute)
      {
          $customer->setValue("hi");
      }
      
      if you are saving using customer object
      $customer->setData('client_dn', 'Hi');
      

      【讨论】:

        猜你喜欢
        • 2022-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        相关资源
        最近更新 更多