0

I have created 2 address attributes using data patch.

Vendor/Module/Setup/Data/Patch/Latitude.php

use Magento\Customer\Model\Indexer\Address\AttributeProvider;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\Set;
use Magento\Eav\Model\Entity\Attribute\SetFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;

class AddLatitudeCustomerAddressAttribute implements DataPatchInterface, PatchRevertableInterface
{
private $moduleDataSetup;
private $customerSetupFactory;
private $attributeSetFactory;
public function __construct(
    ModuleDataSetupInterface $moduleDataSetup,
    CustomerSetupFactory $customerSetupFactory,
    SetFactory $attributeSetFactory
) {
    $this->moduleDataSetup = $moduleDataSetup;
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
}

/**
 * {@inheritdoc}
 */
public function apply()
{
    $this->moduleDataSetup->getConnection()->startSetup();
    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
    $customerEntity = $customerSetup->getEavConfig()->getEntityType(AttributeProvider::ENTITY);
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    
    /** @var $attributeSet Set */
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
    
    $customerSetup->addAttribute(AttributeProvider::ENTITY, 'latitude', [
        'type' => 'varchar',
        'label' => 'Latitude',
        'input' => 'text',
        'source' => '',
        'required' => true,
        'visible' => true,
        'position' => 333,
        'system' => false,
        'backend' => ''
    ]);
    
    $attribute = $customerSetup->getEavConfig()->getAttribute(AttributeProvider::ENTITY, 'latitude');
    $attribute->addData([
        'used_in_forms' => [
            'adminhtml_customer_address',
            'customer_address_edit',
            'customer_register_address'
        ]
    ]);
    $attribute->addData([
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId
    
    ]);
    $attribute->save();

    $this->moduleDataSetup->getConnection()->endSetup();
}

public function revert()
{
    $this->moduleDataSetup->getConnection()->startSetup();
    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
    $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'latitude');

    $this->moduleDataSetup->getConnection()->endSetup();
}

/**
 * {@inheritdoc}
 */
public function getAliases()
{
    return [];
}

/**
 * {@inheritdoc}
 */
public static function getDependencies()
{
    return [
    
    ];
  }
 }

The attribute is created, Now I written an observer to get the custom address attribute.

Vendor/Module/etc/events.xml

<event name="sales_order_save_after">
    <observer name="sales_order_save_after"
              instance="Vendor\Module\Observer\SalesOrderAfterSave" />
</event>

Vendor\Module\Observer\SalesOrderAfterSave.php

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
      $orderData = $observer->getEvent()->getOrder();
      if ($orderData) {
        $shippingAddress = $orderData->getShippingAddress()->getData();
        $latitude= $shippingAddress['latitude'];
       }
    }

In the above code unable to get $latitude value from the shipping address, can anyone help me how to get the custom address attribute. Thanks in advance!!.

Your Answer

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

Browse other questions tagged or ask your own question.