0

I have a problem with saving the custom billing attributes.

I added attributes into the Customer Address Entity table.

all fields of billing address are saved to DB without my custom attribute.

My attribute is saved in the order table, if logged customer edit field is saved too.

But if the logged customer adds a new billing address the attribute (billing address extension attribute) is not saved.

I use https://codeblog.experius.nl/magento-2-add-extra-billing-or-shipping-field-to-the-checkout/ instruction to build my module.

And USE this observer:

namespace Venige\CustomAtt\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Customer\Api\CustomerRepositoryInterface;



class SaveCustomFieldsInOrder implements ObserverInterface
{
    
    
    public function execute(\Magento\Framework\Event\Observer $observer) {
        $order = $observer->getEvent()->getOrder();
        $quote = $observer->getEvent()->getQuote();
    
               $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/SaveCustomFieldsInOrder.log');
                $logger = new \Zend\Log\Logger();
                $logger->addWriter($writer);
                //My custom attribute has value in this moment
                $logger->info('SaveCustomFieldsInOrder: '.$quote->getBillingAddress()->getExtensionAttributes()->getEik()); //My custom attribute has value in this moment
         if ($quote->getBillingAddress()) {
              $order->getBillingAddress()->setEik($quote->getBillingAddress()->getExtensionAttributes()->getEik());
             //$customerAddress->setEik($quote->getBillingAddress()->getExtensionAttributes()->getEik());
              
          }
          if (!$quote->isVirtual()) {            
              $order->getShippingAddress()->setEik($quote->getShippingAddress()->getEik());
          }
          
    
        return $this;
    }
        
       
}

how to save my custom billing address attribute value into customer address in database.

3 Answers 3

0

You need to add the Below code on the address attribute to create time.

->addData(['used_in_forms' => [
                   'adminhtml_customer_address',
                   'adminhtml_customer',
                   'customer_address_edit',
                   'customer_register_address',
                   'customer_address',
                  ]
0
    public function apply()
{

$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]); $customerAddressEntity = $customerSetup->getEavConfig()->getEntityType( 'customer_address'); $attributeSetId = $customerAddressEntity->getDefaultAttributeSetId();

    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
    
    
    $entityTypeId = 'customer_address';
    
         $attributesInfo = [
            'eik' => [
                'label' => 'EIK',
                'type' => 'static',
                'input' => 'text',
                'position' => 71,
                'sort_order' => 71,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'is_used_in_grid' => 1,
                'is_visible_in_grid' => 1,
                'is_filterable_in_grid' => 1,
                'system' => 0,
            ]
         ];
        foreach ($attributesInfo as $attributeCode => $attributeParams) {
            $customerSetup->addAttribute($entityTypeId, $attributeCode, $attributeParams);
            //$customerSetup->->addAttributeToSet($entityTypeId, $setId, null, $attributeCode);
        }

        $attribute = $customerSetup->getEavConfig()->getAttribute($entityTypeId, 'eik');

        $attribute->setData(
            
            'used_in_forms',[               
            'adminhtml_checkout',
            'adminhtml_customer',
            'adminhtml_customer_address',
            'customer_account_edit',
            'customer_address_edit',
            'customer_register_address',
            'customer_account_create',
            'customer_address',
            //],
        ]);
        $attribute->setData('validate_rules', [
                'input_validation' => 1,
                'min_text_length' => 9,
                'max_text_length' => 15,
     
            ]);
        $attribute->addData([
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId,
        ]);
        $this->attribute->save($attribute);

    
    return $this;
}

this is my code to add attribute

0

i dided my self

if($order->getCustomerId()){
        $customer = $this->_customerRepositoryInterface->getById($order->getCustomerId());
        foreach($customer->getAddresses() as $address) {
            $addressID= $address->getId();
            if($addressID==$quote->getBillingAddress()->getCustomerAddressId()){
                $objectManager  = \Magento\Framework\App\ObjectManager::getInstance();
                $addressRepository      = $objectManager->create('\Magento\Customer\Api\AddressRepositoryInterface');
                $addressObject          = $addressRepository->getById($addressID);
                $addressObject->setCustomAttribute('eik',$quote->getBillingAddress()->getExtensionAttributes()->getEik());
                $addressRepository->save($addressObject);
                    
                }
        }       
    }

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.