Custom address attribute: issue on display value magento 2Custom renderer for custom customer address...

Split a number into equal parts given the number of parts

Quitting employee has privileged access to critical information

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

GDAL GetGeoTransform Documentation -- Is there an oversight, or what am I misunderstanding?

Are there other characters in the Star Wars universe who had damaged bodies and needed to wear an outfit like Darth Vader?

Can an earth elemental drown/bury its opponent underground using earth glide?

Deal the cards to the players

School performs periodic password audits. Is my password compromised?

Should I use HTTPS on a domain that will only be used for redirection?

Has Wakanda ever accepted refugees?

Are small insurances worth it

How to disable or uninstall iTunes under High Sierra without disabling SIP

Canadian citizen, on US no-fly list. What can I do in order to be allowed on flights which go through US airspace?

Why would the IRS ask for birth certificates or even audit a small tax return?

function only contains jump discontinuity but is not piecewise continuous

Caulking a corner instead of taping with joint compound?

Why is it "take a leak?"

GPL code private and stolen

Wardrobe above a wall with fuse boxes

Why are special aircraft used for the carriers in the United States Navy?

How can I conditionally format my HTML table?

Can I solder 12/2 Romex to extend wire 5 ft?

Specific Chinese carabiner QA?

Why doesn't "adolescent" take any articles in "listen to adolescent agonising"?



Custom address attribute: issue on display value magento 2


Custom renderer for custom customer address attributeUse getUrl() in form action in Magento1.9?soap v2 for custom customer_address attributeCustomer address attribute value is not saving in Magento 2Magento 2. Update customer attribute outside Setup [Install|Upgrade]DataHow to import custom address attribute?Set custom address attribute value programmaticallymagento 2.2.5: Adding Custom Attribute to Customer Edit Form in AdminMagento 2.3 Can't view module's front end page output?Magento 2 plugin change price of products that have a custom attribute with













1















I have created a custom attribute for customer address. I can see the attribute on admin, I can insert value on frontend, the value saved successfully (and it displayed in address preview) however if I try to edit the address, no value appears.



This is my code. I will appreciate any help.




SandyAddressCustomAttributesBlockCustomerAddressFormEditComment.php




<?php

namespace SandyAddressCustomAttributesBlockCustomerAddressFormEdit;

use MagentoCustomerApiAddressRepositoryInterface;
use MagentoCustomerApiDataAddressInterface;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkViewElementTemplate;
use MagentoCustomerApiDataAddressInterfaceFactory;
use MagentoCustomerModelSession;
use MagentoFrameworkViewElementTemplateContext;

class Comment extends Template
{
/** @var AddressInterface */
private $address;
/** @var AddressRepositoryInterface */
private $addressRepository;
/** @var AddressInterfaceFactory */
private $addressFactory;
/** @var Session */
private $customerSession;

/**
* Comment Constructor
* @param Template|Context $context
* @param AddressRepositoryInterface $addressRepository
* @param AddressInterfaceFactory $addressFactory
* @param Session $session
* @param array $data
*/
public function __construct(
Context $context,
AddressRepositoryInterface $addressRepository,
AddressInterfaceFactory $addressFactory,
Session $session,
array $data = []
)
{
parent::__construct($context, $data);
$this->addressRepository = $addressRepository;
$this->addressFactory = $addressFactory;
$this->customerSession = $session;
}


protected function _prepareLayout()
{
$addressId = $this->getRequest()->getParam('id');

if ($addressId) {
try {
$this->address = $this->addressRepository->getById($addressId);
if ($this->address->getCustomerId() != $this->customerSession->getCustomerId())
$this->address = null;
} catch (NoSuchEntityException $exception) {
$this->address = null;
}
}
if (null === $this->address)
$this->address = $this->addressFactory->create();

return parent::_prepareLayout();
}

/**
* @return string
* @throws MagentoFrameworkExceptionLocalizedException
*/
protected function _toHtml()
{

$customWidgetBlock = $this->getLayout()->createBlock(
'SandyAddressCustomAttributesBlockCustomerWidgetComment'
);

$customWidgetBlock->setAddress($this->address);

return $customWidgetBlock->toHtml();
}
}



SandyAddressCustomAttributesBlockCustomerWidgetComment.php




<?php

namespace SandyAddressCustomAttributesBlockCustomerWidget;

use MagentoCustomerApiAddressMetadataInterface;
use MagentoCustomerApiDataAddressInterface;
use MagentoFrameworkViewElementTemplate;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkPhrase;
use MagentoFrameworkViewElementTemplateContext;

class Comment extends Template
{
/** @var AddressMetadataInterface */
private $addressMetadata;

protected function _construct()
{
parent::_construct();
$this->setTemplate('widget/comment.phtml');
}

/**
* Comment constructor.
* @param Context $context
* @param AddressMetadataInterface $addressMetadata
* @param array $data
*/
public function __construct(
Context $context,
AddressMetadataInterface $addressMetadata,
array $data = []
)
{
parent::__construct($context, $data);
$this->addressMetadata = $addressMetadata;
}


/**
* @return bool
*/
public function isRequired()
{
return $this->getAttribute()
? $this->getAttribute()->isRequired()
: false;
}

/**
* @return string
*/
public function getFieldId()
{
return 'comment';
}

/**
* @return Phrase|string
*/
public function getFieldLabel()
{
return $this->getAttribute()
? $this->getAttribute()->getFrontendLabel()
: __('Comment');
}

/**
* @return string
*/
public function getFieldName()
{
return 'comment';
}

public function getSortOrder()
{
return $this->getAttribute()->getSortOrder();
}

/**
* @return string|null
*/
public function getValue()
{
/** @var AddressInterface $address */
$address = $this->getAddress();
if ($address instanceof AddressInterface) {
return $address->getCustomAttribute('comment')
? $address->getCustomAttribute('comment')->getValue()
: null;
}
return null;
}

private function getAttribute()
{
try {
$attribute = $this->addressMetadata->getAttributeMetadata('comment');
} catch (NoSuchEntityException $exception) {
$attribute = null;
}

return $attribute;
}
}



SandyAddressCustomAttributesPluginCustomerAddressEditPlugin.php




<?php

namespace SandyAddressCustomAttributesPluginCustomer;

use MagentoFrameworkViewLayoutInterface;

class AddressEditPlugin
{

/** @var LayoutInterface */
private $layout;

public function __construct(
LayoutInterface $layout
)
{
$this->layout = $layout;
}

/**
* @param MagentoCustomerBlockAddressEdit $edit
* @param $result
* @return string
*/
public function afterGetNameBlockHtml(
MagentoCustomerBlockAddressEdit $edit,
$result
)
{
$customBlock = $this->layout->createBlock(
'SandyAddressCustomAttributesBlockCustomerAddressFormEditComment',
'sandy_address_custom_attributes_comment'
);

return $result . $customBlock->toHtml();
}
}



view/frontend/templates/widget/comment.phtml




<?php /** @var SandyAddressCustomAttributesBlockCustomerWidgetComment $block */ ?>
<div class="field comment <?php if($block->isRequired()) echo ' required'; echo $block->escapeHtml($block->getSortOrder());?>">
<label class="label" for="<?php echo $block->escapeHtml($block->getFieldId()); ?>">
<span>
<?php echo $block->escapeHtml($block->getFieldLabel()); ?>
</span>
</label>
<div class="control">
<input type="text"
id="<?php echo $block->escapeHtml($block->getFieldId()); ?>"
name="<?php echo $block->escapeHtml($block->getFieldName()); ?>"
value="<?php echo $block->escapeHtml($block->getValue()); ?>"
title="<?php echo $block->escapeHtml($block->getFieldLabel()); ?>"
class="input-text"
<?php /* @escapeNotVerified */ echo $block->escapeHtml($block->getFieldParams()); ?>
<?php if($block->isRequired()) echo 'data-validate="{required:true}"' ?>
/>
</div>
</div>



SandyAddressCustomAttributesSetupInstallData




<?php


namespace SandyAddressCustomAttributesSetup;

use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoCustomerModelCustomer;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoCustomerSetupCustomerSetupFactory;

class InstallData implements InstallDataInterface
{

private $customerSetupFactory;

/**
* Constructor
*
* @param MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
}

/**
* {@inheritdoc}
*/
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

$customerSetup->addAttribute(MagentoCustomerModelIndexerAddressAttributeProvider::ENTITY, 'comment', [
'label' => 'Comment',
'input' => 'textarea',
'type' => 'text',
'source' => '',
'required' => false,
'position' => 1001,
'visible' => true,
'system' => false,
'is_used_in_grid' => false,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => false,
'is_searchable_in_grid' => false,
'backend' => ''
]);


$attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'comment')
->addData(['used_in_forms' => [
'adminhtml_customer_address',
'customer_address_edit',
'customer_register_address'
]]);
$attribute->save();
}
}









share|improve this question





























    1















    I have created a custom attribute for customer address. I can see the attribute on admin, I can insert value on frontend, the value saved successfully (and it displayed in address preview) however if I try to edit the address, no value appears.



    This is my code. I will appreciate any help.




    SandyAddressCustomAttributesBlockCustomerAddressFormEditComment.php




    <?php

    namespace SandyAddressCustomAttributesBlockCustomerAddressFormEdit;

    use MagentoCustomerApiAddressRepositoryInterface;
    use MagentoCustomerApiDataAddressInterface;
    use MagentoFrameworkExceptionNoSuchEntityException;
    use MagentoFrameworkViewElementTemplate;
    use MagentoCustomerApiDataAddressInterfaceFactory;
    use MagentoCustomerModelSession;
    use MagentoFrameworkViewElementTemplateContext;

    class Comment extends Template
    {
    /** @var AddressInterface */
    private $address;
    /** @var AddressRepositoryInterface */
    private $addressRepository;
    /** @var AddressInterfaceFactory */
    private $addressFactory;
    /** @var Session */
    private $customerSession;

    /**
    * Comment Constructor
    * @param Template|Context $context
    * @param AddressRepositoryInterface $addressRepository
    * @param AddressInterfaceFactory $addressFactory
    * @param Session $session
    * @param array $data
    */
    public function __construct(
    Context $context,
    AddressRepositoryInterface $addressRepository,
    AddressInterfaceFactory $addressFactory,
    Session $session,
    array $data = []
    )
    {
    parent::__construct($context, $data);
    $this->addressRepository = $addressRepository;
    $this->addressFactory = $addressFactory;
    $this->customerSession = $session;
    }


    protected function _prepareLayout()
    {
    $addressId = $this->getRequest()->getParam('id');

    if ($addressId) {
    try {
    $this->address = $this->addressRepository->getById($addressId);
    if ($this->address->getCustomerId() != $this->customerSession->getCustomerId())
    $this->address = null;
    } catch (NoSuchEntityException $exception) {
    $this->address = null;
    }
    }
    if (null === $this->address)
    $this->address = $this->addressFactory->create();

    return parent::_prepareLayout();
    }

    /**
    * @return string
    * @throws MagentoFrameworkExceptionLocalizedException
    */
    protected function _toHtml()
    {

    $customWidgetBlock = $this->getLayout()->createBlock(
    'SandyAddressCustomAttributesBlockCustomerWidgetComment'
    );

    $customWidgetBlock->setAddress($this->address);

    return $customWidgetBlock->toHtml();
    }
    }



    SandyAddressCustomAttributesBlockCustomerWidgetComment.php




    <?php

    namespace SandyAddressCustomAttributesBlockCustomerWidget;

    use MagentoCustomerApiAddressMetadataInterface;
    use MagentoCustomerApiDataAddressInterface;
    use MagentoFrameworkViewElementTemplate;
    use MagentoFrameworkExceptionNoSuchEntityException;
    use MagentoFrameworkPhrase;
    use MagentoFrameworkViewElementTemplateContext;

    class Comment extends Template
    {
    /** @var AddressMetadataInterface */
    private $addressMetadata;

    protected function _construct()
    {
    parent::_construct();
    $this->setTemplate('widget/comment.phtml');
    }

    /**
    * Comment constructor.
    * @param Context $context
    * @param AddressMetadataInterface $addressMetadata
    * @param array $data
    */
    public function __construct(
    Context $context,
    AddressMetadataInterface $addressMetadata,
    array $data = []
    )
    {
    parent::__construct($context, $data);
    $this->addressMetadata = $addressMetadata;
    }


    /**
    * @return bool
    */
    public function isRequired()
    {
    return $this->getAttribute()
    ? $this->getAttribute()->isRequired()
    : false;
    }

    /**
    * @return string
    */
    public function getFieldId()
    {
    return 'comment';
    }

    /**
    * @return Phrase|string
    */
    public function getFieldLabel()
    {
    return $this->getAttribute()
    ? $this->getAttribute()->getFrontendLabel()
    : __('Comment');
    }

    /**
    * @return string
    */
    public function getFieldName()
    {
    return 'comment';
    }

    public function getSortOrder()
    {
    return $this->getAttribute()->getSortOrder();
    }

    /**
    * @return string|null
    */
    public function getValue()
    {
    /** @var AddressInterface $address */
    $address = $this->getAddress();
    if ($address instanceof AddressInterface) {
    return $address->getCustomAttribute('comment')
    ? $address->getCustomAttribute('comment')->getValue()
    : null;
    }
    return null;
    }

    private function getAttribute()
    {
    try {
    $attribute = $this->addressMetadata->getAttributeMetadata('comment');
    } catch (NoSuchEntityException $exception) {
    $attribute = null;
    }

    return $attribute;
    }
    }



    SandyAddressCustomAttributesPluginCustomerAddressEditPlugin.php




    <?php

    namespace SandyAddressCustomAttributesPluginCustomer;

    use MagentoFrameworkViewLayoutInterface;

    class AddressEditPlugin
    {

    /** @var LayoutInterface */
    private $layout;

    public function __construct(
    LayoutInterface $layout
    )
    {
    $this->layout = $layout;
    }

    /**
    * @param MagentoCustomerBlockAddressEdit $edit
    * @param $result
    * @return string
    */
    public function afterGetNameBlockHtml(
    MagentoCustomerBlockAddressEdit $edit,
    $result
    )
    {
    $customBlock = $this->layout->createBlock(
    'SandyAddressCustomAttributesBlockCustomerAddressFormEditComment',
    'sandy_address_custom_attributes_comment'
    );

    return $result . $customBlock->toHtml();
    }
    }



    view/frontend/templates/widget/comment.phtml




    <?php /** @var SandyAddressCustomAttributesBlockCustomerWidgetComment $block */ ?>
    <div class="field comment <?php if($block->isRequired()) echo ' required'; echo $block->escapeHtml($block->getSortOrder());?>">
    <label class="label" for="<?php echo $block->escapeHtml($block->getFieldId()); ?>">
    <span>
    <?php echo $block->escapeHtml($block->getFieldLabel()); ?>
    </span>
    </label>
    <div class="control">
    <input type="text"
    id="<?php echo $block->escapeHtml($block->getFieldId()); ?>"
    name="<?php echo $block->escapeHtml($block->getFieldName()); ?>"
    value="<?php echo $block->escapeHtml($block->getValue()); ?>"
    title="<?php echo $block->escapeHtml($block->getFieldLabel()); ?>"
    class="input-text"
    <?php /* @escapeNotVerified */ echo $block->escapeHtml($block->getFieldParams()); ?>
    <?php if($block->isRequired()) echo 'data-validate="{required:true}"' ?>
    />
    </div>
    </div>



    SandyAddressCustomAttributesSetupInstallData




    <?php


    namespace SandyAddressCustomAttributesSetup;

    use MagentoFrameworkSetupInstallDataInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    use MagentoCustomerModelCustomer;
    use MagentoFrameworkSetupModuleContextInterface;
    use MagentoCustomerSetupCustomerSetupFactory;

    class InstallData implements InstallDataInterface
    {

    private $customerSetupFactory;

    /**
    * Constructor
    *
    * @param MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory
    */
    public function __construct(
    CustomerSetupFactory $customerSetupFactory
    ) {
    $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
    * {@inheritdoc}
    */
    public function install(
    ModuleDataSetupInterface $setup,
    ModuleContextInterface $context
    ) {
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $customerSetup->addAttribute(MagentoCustomerModelIndexerAddressAttributeProvider::ENTITY, 'comment', [
    'label' => 'Comment',
    'input' => 'textarea',
    'type' => 'text',
    'source' => '',
    'required' => false,
    'position' => 1001,
    'visible' => true,
    'system' => false,
    'is_used_in_grid' => false,
    'is_visible_in_grid' => false,
    'is_filterable_in_grid' => false,
    'is_searchable_in_grid' => false,
    'backend' => ''
    ]);


    $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'comment')
    ->addData(['used_in_forms' => [
    'adminhtml_customer_address',
    'customer_address_edit',
    'customer_register_address'
    ]]);
    $attribute->save();
    }
    }









    share|improve this question



























      1












      1








      1


      1






      I have created a custom attribute for customer address. I can see the attribute on admin, I can insert value on frontend, the value saved successfully (and it displayed in address preview) however if I try to edit the address, no value appears.



      This is my code. I will appreciate any help.




      SandyAddressCustomAttributesBlockCustomerAddressFormEditComment.php




      <?php

      namespace SandyAddressCustomAttributesBlockCustomerAddressFormEdit;

      use MagentoCustomerApiAddressRepositoryInterface;
      use MagentoCustomerApiDataAddressInterface;
      use MagentoFrameworkExceptionNoSuchEntityException;
      use MagentoFrameworkViewElementTemplate;
      use MagentoCustomerApiDataAddressInterfaceFactory;
      use MagentoCustomerModelSession;
      use MagentoFrameworkViewElementTemplateContext;

      class Comment extends Template
      {
      /** @var AddressInterface */
      private $address;
      /** @var AddressRepositoryInterface */
      private $addressRepository;
      /** @var AddressInterfaceFactory */
      private $addressFactory;
      /** @var Session */
      private $customerSession;

      /**
      * Comment Constructor
      * @param Template|Context $context
      * @param AddressRepositoryInterface $addressRepository
      * @param AddressInterfaceFactory $addressFactory
      * @param Session $session
      * @param array $data
      */
      public function __construct(
      Context $context,
      AddressRepositoryInterface $addressRepository,
      AddressInterfaceFactory $addressFactory,
      Session $session,
      array $data = []
      )
      {
      parent::__construct($context, $data);
      $this->addressRepository = $addressRepository;
      $this->addressFactory = $addressFactory;
      $this->customerSession = $session;
      }


      protected function _prepareLayout()
      {
      $addressId = $this->getRequest()->getParam('id');

      if ($addressId) {
      try {
      $this->address = $this->addressRepository->getById($addressId);
      if ($this->address->getCustomerId() != $this->customerSession->getCustomerId())
      $this->address = null;
      } catch (NoSuchEntityException $exception) {
      $this->address = null;
      }
      }
      if (null === $this->address)
      $this->address = $this->addressFactory->create();

      return parent::_prepareLayout();
      }

      /**
      * @return string
      * @throws MagentoFrameworkExceptionLocalizedException
      */
      protected function _toHtml()
      {

      $customWidgetBlock = $this->getLayout()->createBlock(
      'SandyAddressCustomAttributesBlockCustomerWidgetComment'
      );

      $customWidgetBlock->setAddress($this->address);

      return $customWidgetBlock->toHtml();
      }
      }



      SandyAddressCustomAttributesBlockCustomerWidgetComment.php




      <?php

      namespace SandyAddressCustomAttributesBlockCustomerWidget;

      use MagentoCustomerApiAddressMetadataInterface;
      use MagentoCustomerApiDataAddressInterface;
      use MagentoFrameworkViewElementTemplate;
      use MagentoFrameworkExceptionNoSuchEntityException;
      use MagentoFrameworkPhrase;
      use MagentoFrameworkViewElementTemplateContext;

      class Comment extends Template
      {
      /** @var AddressMetadataInterface */
      private $addressMetadata;

      protected function _construct()
      {
      parent::_construct();
      $this->setTemplate('widget/comment.phtml');
      }

      /**
      * Comment constructor.
      * @param Context $context
      * @param AddressMetadataInterface $addressMetadata
      * @param array $data
      */
      public function __construct(
      Context $context,
      AddressMetadataInterface $addressMetadata,
      array $data = []
      )
      {
      parent::__construct($context, $data);
      $this->addressMetadata = $addressMetadata;
      }


      /**
      * @return bool
      */
      public function isRequired()
      {
      return $this->getAttribute()
      ? $this->getAttribute()->isRequired()
      : false;
      }

      /**
      * @return string
      */
      public function getFieldId()
      {
      return 'comment';
      }

      /**
      * @return Phrase|string
      */
      public function getFieldLabel()
      {
      return $this->getAttribute()
      ? $this->getAttribute()->getFrontendLabel()
      : __('Comment');
      }

      /**
      * @return string
      */
      public function getFieldName()
      {
      return 'comment';
      }

      public function getSortOrder()
      {
      return $this->getAttribute()->getSortOrder();
      }

      /**
      * @return string|null
      */
      public function getValue()
      {
      /** @var AddressInterface $address */
      $address = $this->getAddress();
      if ($address instanceof AddressInterface) {
      return $address->getCustomAttribute('comment')
      ? $address->getCustomAttribute('comment')->getValue()
      : null;
      }
      return null;
      }

      private function getAttribute()
      {
      try {
      $attribute = $this->addressMetadata->getAttributeMetadata('comment');
      } catch (NoSuchEntityException $exception) {
      $attribute = null;
      }

      return $attribute;
      }
      }



      SandyAddressCustomAttributesPluginCustomerAddressEditPlugin.php




      <?php

      namespace SandyAddressCustomAttributesPluginCustomer;

      use MagentoFrameworkViewLayoutInterface;

      class AddressEditPlugin
      {

      /** @var LayoutInterface */
      private $layout;

      public function __construct(
      LayoutInterface $layout
      )
      {
      $this->layout = $layout;
      }

      /**
      * @param MagentoCustomerBlockAddressEdit $edit
      * @param $result
      * @return string
      */
      public function afterGetNameBlockHtml(
      MagentoCustomerBlockAddressEdit $edit,
      $result
      )
      {
      $customBlock = $this->layout->createBlock(
      'SandyAddressCustomAttributesBlockCustomerAddressFormEditComment',
      'sandy_address_custom_attributes_comment'
      );

      return $result . $customBlock->toHtml();
      }
      }



      view/frontend/templates/widget/comment.phtml




      <?php /** @var SandyAddressCustomAttributesBlockCustomerWidgetComment $block */ ?>
      <div class="field comment <?php if($block->isRequired()) echo ' required'; echo $block->escapeHtml($block->getSortOrder());?>">
      <label class="label" for="<?php echo $block->escapeHtml($block->getFieldId()); ?>">
      <span>
      <?php echo $block->escapeHtml($block->getFieldLabel()); ?>
      </span>
      </label>
      <div class="control">
      <input type="text"
      id="<?php echo $block->escapeHtml($block->getFieldId()); ?>"
      name="<?php echo $block->escapeHtml($block->getFieldName()); ?>"
      value="<?php echo $block->escapeHtml($block->getValue()); ?>"
      title="<?php echo $block->escapeHtml($block->getFieldLabel()); ?>"
      class="input-text"
      <?php /* @escapeNotVerified */ echo $block->escapeHtml($block->getFieldParams()); ?>
      <?php if($block->isRequired()) echo 'data-validate="{required:true}"' ?>
      />
      </div>
      </div>



      SandyAddressCustomAttributesSetupInstallData




      <?php


      namespace SandyAddressCustomAttributesSetup;

      use MagentoFrameworkSetupInstallDataInterface;
      use MagentoFrameworkSetupModuleDataSetupInterface;
      use MagentoCustomerModelCustomer;
      use MagentoFrameworkSetupModuleContextInterface;
      use MagentoCustomerSetupCustomerSetupFactory;

      class InstallData implements InstallDataInterface
      {

      private $customerSetupFactory;

      /**
      * Constructor
      *
      * @param MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory
      */
      public function __construct(
      CustomerSetupFactory $customerSetupFactory
      ) {
      $this->customerSetupFactory = $customerSetupFactory;
      }

      /**
      * {@inheritdoc}
      */
      public function install(
      ModuleDataSetupInterface $setup,
      ModuleContextInterface $context
      ) {
      $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

      $customerSetup->addAttribute(MagentoCustomerModelIndexerAddressAttributeProvider::ENTITY, 'comment', [
      'label' => 'Comment',
      'input' => 'textarea',
      'type' => 'text',
      'source' => '',
      'required' => false,
      'position' => 1001,
      'visible' => true,
      'system' => false,
      'is_used_in_grid' => false,
      'is_visible_in_grid' => false,
      'is_filterable_in_grid' => false,
      'is_searchable_in_grid' => false,
      'backend' => ''
      ]);


      $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'comment')
      ->addData(['used_in_forms' => [
      'adminhtml_customer_address',
      'customer_address_edit',
      'customer_register_address'
      ]]);
      $attribute->save();
      }
      }









      share|improve this question
















      I have created a custom attribute for customer address. I can see the attribute on admin, I can insert value on frontend, the value saved successfully (and it displayed in address preview) however if I try to edit the address, no value appears.



      This is my code. I will appreciate any help.




      SandyAddressCustomAttributesBlockCustomerAddressFormEditComment.php




      <?php

      namespace SandyAddressCustomAttributesBlockCustomerAddressFormEdit;

      use MagentoCustomerApiAddressRepositoryInterface;
      use MagentoCustomerApiDataAddressInterface;
      use MagentoFrameworkExceptionNoSuchEntityException;
      use MagentoFrameworkViewElementTemplate;
      use MagentoCustomerApiDataAddressInterfaceFactory;
      use MagentoCustomerModelSession;
      use MagentoFrameworkViewElementTemplateContext;

      class Comment extends Template
      {
      /** @var AddressInterface */
      private $address;
      /** @var AddressRepositoryInterface */
      private $addressRepository;
      /** @var AddressInterfaceFactory */
      private $addressFactory;
      /** @var Session */
      private $customerSession;

      /**
      * Comment Constructor
      * @param Template|Context $context
      * @param AddressRepositoryInterface $addressRepository
      * @param AddressInterfaceFactory $addressFactory
      * @param Session $session
      * @param array $data
      */
      public function __construct(
      Context $context,
      AddressRepositoryInterface $addressRepository,
      AddressInterfaceFactory $addressFactory,
      Session $session,
      array $data = []
      )
      {
      parent::__construct($context, $data);
      $this->addressRepository = $addressRepository;
      $this->addressFactory = $addressFactory;
      $this->customerSession = $session;
      }


      protected function _prepareLayout()
      {
      $addressId = $this->getRequest()->getParam('id');

      if ($addressId) {
      try {
      $this->address = $this->addressRepository->getById($addressId);
      if ($this->address->getCustomerId() != $this->customerSession->getCustomerId())
      $this->address = null;
      } catch (NoSuchEntityException $exception) {
      $this->address = null;
      }
      }
      if (null === $this->address)
      $this->address = $this->addressFactory->create();

      return parent::_prepareLayout();
      }

      /**
      * @return string
      * @throws MagentoFrameworkExceptionLocalizedException
      */
      protected function _toHtml()
      {

      $customWidgetBlock = $this->getLayout()->createBlock(
      'SandyAddressCustomAttributesBlockCustomerWidgetComment'
      );

      $customWidgetBlock->setAddress($this->address);

      return $customWidgetBlock->toHtml();
      }
      }



      SandyAddressCustomAttributesBlockCustomerWidgetComment.php




      <?php

      namespace SandyAddressCustomAttributesBlockCustomerWidget;

      use MagentoCustomerApiAddressMetadataInterface;
      use MagentoCustomerApiDataAddressInterface;
      use MagentoFrameworkViewElementTemplate;
      use MagentoFrameworkExceptionNoSuchEntityException;
      use MagentoFrameworkPhrase;
      use MagentoFrameworkViewElementTemplateContext;

      class Comment extends Template
      {
      /** @var AddressMetadataInterface */
      private $addressMetadata;

      protected function _construct()
      {
      parent::_construct();
      $this->setTemplate('widget/comment.phtml');
      }

      /**
      * Comment constructor.
      * @param Context $context
      * @param AddressMetadataInterface $addressMetadata
      * @param array $data
      */
      public function __construct(
      Context $context,
      AddressMetadataInterface $addressMetadata,
      array $data = []
      )
      {
      parent::__construct($context, $data);
      $this->addressMetadata = $addressMetadata;
      }


      /**
      * @return bool
      */
      public function isRequired()
      {
      return $this->getAttribute()
      ? $this->getAttribute()->isRequired()
      : false;
      }

      /**
      * @return string
      */
      public function getFieldId()
      {
      return 'comment';
      }

      /**
      * @return Phrase|string
      */
      public function getFieldLabel()
      {
      return $this->getAttribute()
      ? $this->getAttribute()->getFrontendLabel()
      : __('Comment');
      }

      /**
      * @return string
      */
      public function getFieldName()
      {
      return 'comment';
      }

      public function getSortOrder()
      {
      return $this->getAttribute()->getSortOrder();
      }

      /**
      * @return string|null
      */
      public function getValue()
      {
      /** @var AddressInterface $address */
      $address = $this->getAddress();
      if ($address instanceof AddressInterface) {
      return $address->getCustomAttribute('comment')
      ? $address->getCustomAttribute('comment')->getValue()
      : null;
      }
      return null;
      }

      private function getAttribute()
      {
      try {
      $attribute = $this->addressMetadata->getAttributeMetadata('comment');
      } catch (NoSuchEntityException $exception) {
      $attribute = null;
      }

      return $attribute;
      }
      }



      SandyAddressCustomAttributesPluginCustomerAddressEditPlugin.php




      <?php

      namespace SandyAddressCustomAttributesPluginCustomer;

      use MagentoFrameworkViewLayoutInterface;

      class AddressEditPlugin
      {

      /** @var LayoutInterface */
      private $layout;

      public function __construct(
      LayoutInterface $layout
      )
      {
      $this->layout = $layout;
      }

      /**
      * @param MagentoCustomerBlockAddressEdit $edit
      * @param $result
      * @return string
      */
      public function afterGetNameBlockHtml(
      MagentoCustomerBlockAddressEdit $edit,
      $result
      )
      {
      $customBlock = $this->layout->createBlock(
      'SandyAddressCustomAttributesBlockCustomerAddressFormEditComment',
      'sandy_address_custom_attributes_comment'
      );

      return $result . $customBlock->toHtml();
      }
      }



      view/frontend/templates/widget/comment.phtml




      <?php /** @var SandyAddressCustomAttributesBlockCustomerWidgetComment $block */ ?>
      <div class="field comment <?php if($block->isRequired()) echo ' required'; echo $block->escapeHtml($block->getSortOrder());?>">
      <label class="label" for="<?php echo $block->escapeHtml($block->getFieldId()); ?>">
      <span>
      <?php echo $block->escapeHtml($block->getFieldLabel()); ?>
      </span>
      </label>
      <div class="control">
      <input type="text"
      id="<?php echo $block->escapeHtml($block->getFieldId()); ?>"
      name="<?php echo $block->escapeHtml($block->getFieldName()); ?>"
      value="<?php echo $block->escapeHtml($block->getValue()); ?>"
      title="<?php echo $block->escapeHtml($block->getFieldLabel()); ?>"
      class="input-text"
      <?php /* @escapeNotVerified */ echo $block->escapeHtml($block->getFieldParams()); ?>
      <?php if($block->isRequired()) echo 'data-validate="{required:true}"' ?>
      />
      </div>
      </div>



      SandyAddressCustomAttributesSetupInstallData




      <?php


      namespace SandyAddressCustomAttributesSetup;

      use MagentoFrameworkSetupInstallDataInterface;
      use MagentoFrameworkSetupModuleDataSetupInterface;
      use MagentoCustomerModelCustomer;
      use MagentoFrameworkSetupModuleContextInterface;
      use MagentoCustomerSetupCustomerSetupFactory;

      class InstallData implements InstallDataInterface
      {

      private $customerSetupFactory;

      /**
      * Constructor
      *
      * @param MagentoCustomerSetupCustomerSetupFactory $customerSetupFactory
      */
      public function __construct(
      CustomerSetupFactory $customerSetupFactory
      ) {
      $this->customerSetupFactory = $customerSetupFactory;
      }

      /**
      * {@inheritdoc}
      */
      public function install(
      ModuleDataSetupInterface $setup,
      ModuleContextInterface $context
      ) {
      $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

      $customerSetup->addAttribute(MagentoCustomerModelIndexerAddressAttributeProvider::ENTITY, 'comment', [
      'label' => 'Comment',
      'input' => 'textarea',
      'type' => 'text',
      'source' => '',
      'required' => false,
      'position' => 1001,
      'visible' => true,
      'system' => false,
      'is_used_in_grid' => false,
      'is_visible_in_grid' => false,
      'is_filterable_in_grid' => false,
      'is_searchable_in_grid' => false,
      'backend' => ''
      ]);


      $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'comment')
      ->addData(['used_in_forms' => [
      'adminhtml_customer_address',
      'customer_address_edit',
      'customer_register_address'
      ]]);
      $attribute->save();
      }
      }






      magento2.3 customer-attribute customer-address






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 10 mins ago









      HIren Kadivar

      716214




      716214










      asked 10 hours ago









      Sandy LampropoulouSandy Lampropoulou

      6




      6






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f264766%2fcustom-address-attribute-issue-on-display-value-magento-2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f264766%2fcustom-address-attribute-issue-on-display-value-magento-2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

          How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

          夢乃愛華...