0

I have created a custom customer attribute, and while create and update customer from backend the value will be saved in Database.

But while do the same thing from front-end the value is not able to save in Database. Moreover, the backend saved value can not able to get using the customer load query.

In customer_form_attribute for that particular attribute have the following code adminhtml_customer,customer_account_edit,customer_account_create

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
         if (version_compare($context->getVersion(), '0.0.4', '<')) {
            $setup->startSetup();

            /** @var CustomerSetup $customerSetup */
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
            $customerSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'order_limit',
                [
                    'visible' => true
                ]
            );
            //$forms = ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit'];
            $forms = ['adminhtml_customer','customer_account_edit','customer_account_create','adminhtml_checkout','checkout_register'];

            $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'order_limit');
            $attribute->setData('used_in_forms', $forms);

            $attribute->save();
            $setup->endSetup();
        }    
    }

Kindly share your thoughts and idea.

Thanks in advance for your support

  • Can you paste the InstallData.php / UpdateData.php that contains the creation of these custom customer attribtues – nikin Dec 12 '18 at 15:25
1

------- CREATE CUSTOM ATTRIBUTE --------

Step 1 : create app/code/Dckap/CustomAttributes

Step 2 : Create app/code/Dckap/CustomAttributes/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Dckap_CustomAttributes" setup_version="1.0.0">
    </module>
</config>

Step 3 : Create app/code/Dckap/CustomAttributes/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Dckap_CustomAttributes',
    __DIR__
);

Step 4 : Create app/code/Dckap/CustomAttributes/Setup/InstallData.php

<?php

namespace Dckap\CustomAttributes\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        if (version_compare($context->getVersion(), '1.0.0') < 0){

                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $customerSetup = $objectManager->create('Dckap\CustomAttributes\Setup\CustomerSetup');
                $customerSetup->installAttributes($customerSetup);



        }

    }
}

Step 5 : Create app/code/Dckap/CustomAttributes/Setup/CustomerSetup.php

<?php

namespace Dckap\CustomAttributes\Setup;

use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Setup\Context;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;

class CustomerSetup extends EavSetup {

    protected $eavConfig;

    public function __construct(
        ModuleDataSetupInterface $setup,
        Context $context,
        CacheInterface $cache,
        CollectionFactory $attrGroupCollectionFactory,
        Config $eavConfig
        ) {
        $this -> eavConfig = $eavConfig;
        parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory);
    } 

    public function installAttributes($customerSetup) {
        $this -> installCustomerAttributes($customerSetup);
        $this -> installCustomerAddressAttributes($customerSetup);
    } 

    public function installCustomerAttributes($customerSetup) {
        $customerSetup -> addAttribute(\Magento\Customer\Model\Customer::ENTITY,
            'custom_attribute_value',
            [
            'label' => 'Custom Attributes',
            'system' => 0,
            'position' => 100,
            'sort_order' =>100,
            'visible' =>  true,
            'note' => '',
            'type' => 'varchar',
            'input' => 'text',
            ]
            );

        $customerSetup -> getEavConfig() -> getAttribute('customer', 'custom_attribute_value')->setData('is_user_defined',1)->setData('is_required',0)->setData('default_value','')->setData('used_in_forms', ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit']) -> save();               
    } 

    public function installCustomerAddressAttributes($customerSetup) {          
    } 

    public function getEavConfig() {
        return $this -> eavConfig;
    } 
} 

Finaly run command

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
php bin/magento cache:flush

enter image description here

----- SHOW CUSTOM ATTRIBUTE IN REGISTRATION FORM ------

Navigate to app/design/frontend/vendor_name/theme_name/Magento_Customer/templates/register.phtml

<div class="field required">
     <label for="custom_attribute_value" class="label"><span><?= $block->escapeHtml(__('Custom Attribute')) ?></span></label>
      <div class="control">
          <input type="text" name="custom_attribute_value" class="custom_attribute_value" id="custom_attribute_value">
      </div>
 </div>

Finaly run command

php bin/magento cache:clean
php bin/magento cache:flush

enter image description here

0

If your install data script successfully installed your custom attribute(order_limit), now you just need to override addition information phtml file and set your custom attribute in that.

Create Stackexchange/CustomAttribute/view/frontend/layout/customer_account_edit.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="customer_account"/>
    <body>
        <referenceBlock name="head.components">
            <block class="Magento\Framework\View\Element\Js\Components" name="customer_account_edit_head_components" template="Magento_Customer::js/components.phtml"/>
        </referenceBlock>
        <referenceBlock name="page.main.title">
            <action method="setPageTitle">
                <argument translate="true" name="title" xsi:type="string">Edit Account Information</argument>
            </action>
        </referenceBlock>
        <referenceContainer name="content">
            <block class="Magento\Customer\Block\Form\Edit" name="customer_edit" template="Stackexchange_CustomAttribute::form/edit.phtml" cacheable="false">
                <container name="form.additional.info" as="form_additional_info"/>
            </block>
        </referenceContainer>
    </body>
</page>

Create Stackexchange/CustomAttribute/view/frontend/templates/form/edit.phtml

<div class="field orderlimit">
    <label class="label" for="order_limit"><span><?php /* @escapeNotVerified */ echo __('Order Limit') ?></span></label>

    <input type="text" id="order_limit" name="order_limit" value="<?= $block->getCustomer()->getCustomAttribute("order_limit")->getValue() ?>" title="<?php /* @escapeNotVerified */ echo __('Order Limit') ?>" class="input-text" autocomplete="off" />
</div>

Clear cache and review frontend customer account registration page.

Source Code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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