0

How can i add new parameter to an API for example /V1/customers for register a new atribute phone for example.

1 Answer 1

1

Step 1:Create etc/registration.php file

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Sunarc_Custom',
    __DIR__
);

Step 2: Create etc/module.xml file.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Sunarc_Custom" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Backend"/>
        </sequence>
    </module>
</config>

Step 3: Create a customer attribute named Phone Number in Setup/InstallData.php file

<?php

namespace Sunarc\Custom\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Customer\Model\Customer;

class InstallData implements InstallDataInterface
{
    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * @var \Magento\Eav\Model\Entity\Attribute\SetFactory
     */
    private $attributeSetFactory;

    /**
     * [__construct]
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory                          $attributeSetFactory
    )
    {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }

    /**
     * Installs DB schema for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     *
     * @return void
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface   $context
    )
    {
        $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,
            'phone_number',
            [
                'type' => 'varchar',
                'label' => 'Phone Number',
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'position' => 366,
                'system' => false,
                'global' => true,
                'is_used_in_grid' => true,
                'is_visible_in_grid' => true,
                'is_filterable_in_grid' => false,
                'is_searchable_in_grid' => false
            ]
        );

        //add attribute to attribute set
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'phone_number')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']]);

        $attribute->save();

    }
}

Step 4: Run commands

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
rm -rf var/di/* var/generation/* var/cache/* var/page_cache/* var/view_preprocessed/* var/composer_home/cache/*
php bin/magento cache:clean
php bin/magento indexer:reindex
chmod -R 755 var/ pub/ generated/

Step 5:Create customers through Rest API with custom attribute Api used : <base_url>/rest/all/V1/customers

Body :

{ 
  "customer": 
  {
  "email": "[email protected]", 
  "firstname": "x", 
  "lastname": "y", 
  "website_id":1, 
  "group_id":1, 
  "custom_attributes": 
  [ 
    { 
    "attribute_code": "phone_number", 
    "value": "7896541230" 
    } 
  ]

},
"password": "admin@123"
}

Response:

{
    "id": 7,
    "group_id": 1,
    "created_at": "2023-01-09 07:57:05",
    "updated_at": "2023-01-09 07:57:05",
    "created_in": "Default Store View",
    "email": "[email protected]",
    "firstname": "x",
    "lastname": "y",
    "store_id": 1,
    "website_id": 1,
    "addresses": [],
    "disable_auto_group_change": 0,
    "extension_attributes": {
        "is_subscribed": false
    },
    "custom_attributes": [
        {
            "attribute_code": "phone_number",
            "value": "7896541230"
        }
    ]
}
1
  • Whatever you are hoping to accomplish, chmod 777 is wrong and dangerous. You absolutely do not want to grant write access to executable or system files to all users under any circumstances. You will want to revert to sane permissions ASAP (for your use case, probably chmod 755) and learn about the Unix permissions model before you try to use it again. If this happened on a system with Internet access, check whether an intruder could have exploited this to escalate their privileges.
    – tripleee
    Jan 9, 2023 at 9:11

Your Answer

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

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