0

Namespace/Module/setup/installData.php

$customerSetup->addAttribute(Customer::ENTITY,'credit_type',
                [
                    'type' => 'int',
                    'label' => 'Credit Type requested',
                    'input' => 'select',
                    'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
                    'required' => false,
                    'default' => '0',
                    'sort_order' => 167,
                    'system' => false,
                    'position' => 167,
                    'adminhtml_only' => 1,
                    'option' =>
                        array (
                            'values' =>
                                array (
                                    271 => 'Credit Card',
                                    272 => 'Paypal',
                                    273 => 'Master Card',
                                    274 => 'Visa',
                                ),
                        ),
                ]
            );
            $myAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'credit_type');
            // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
            $myAttribute->setData(
                'used_in_forms', ['adminhtml_customer', 'customer_account_create','wholesale_account_create', 'customer_account_edit']);
            $myAttribute->save();

My template.phtml file code:

       <div class="control">
            <select name="mySelect" id="myselect" title="<?= $block->escapeHtml(__('Requested Credit Type')) ?>" class="validate-select">
                <option disabled selected value> -- Please Select Credit Type Requested -- </option>
                <option value="271">Credit Card</option>
                <option value="272">Open Terms</option>
                <option value="273">Mega / NBA</option>
                <option value="274">Cantrex</option>
            </select>
        </div>

Data doesn't store in backend

2 Answers 2

0

Are you talking about Magento 2? But I see the path from Magento 1

Namespace/Module/sql/module_steup/installData.php

It’s not quite clear what you want, but I see that you’ve left out the attribute set and the customer group.

$myAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'credit_type');
$myAttribute->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create','wholesale_account_create', 'customer_account_edit']);
$attribute->addData([
                      'attribute_set_id' => 1,
                      'attribute_group_id' => 1
                    ]);
$myAttribute->save();

Remove the module from the setup_module table and install again.

1
  • Please check my updated question. Commented Jan 18, 2019 at 14:19
0

-> Add bellow installation script in your module at path app/code/{Namespace}

/{Module-Name}/Setup/InstallData.php


<?php
/**
 * @author      AR
 * @category    AR
 * @package     AR_CustomerAttribute
 */
namespace {Namespace}\{Module-name}\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{

    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }


    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(Customer::ENTITY, 'customer_type', [

            'type'          => 'static',
            'label'         => 'Customer Type',
            'input'         => 'select',
            'source'        => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
            'required'      => false,
            'sort_order'    => 210,
            'visible'       => false,
            'system'        => false,
            'validate_rules'=> 'a:0:{}',
            'position'      => 210,
            'admin_checkout' => 1,
            'option'         => ['values' => ['Buyer', 'Seller']],
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_type')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],
        ]);

        $attribute->save();


    }
}

-> it's working for me please use above code.

3
  • I have four values: ['values' => ['Buyer', 'Seller','creditcard','paypal']], Commented Jan 18, 2019 at 14:18
  • Yes you can add ['values' => ['Buyer', 'Seller','creditcard','paypal']], it's working. Commented Jan 19, 2019 at 5:02
  • In .phtml file, how can I use that values: <div class="control"> <select name="mySelect" id="myselect" title="<?= $block->escapeHtml(__('Requested Credit Type')) ?>" class="validate-select"> <option disabled selected value> -- Please Select Credit Type Requested -- </option> <option value="271">Credit Card</option> <option value="272">Open Terms</option> <option value="273">Mega / NBA</option> <option value="274">Cantrex</option> </select> </div> Commented Jan 21, 2019 at 13:58

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.