2

I am stuck. I hope someone had already solved my problem. The magento docu does not help me any further.

I added some new ExtensionAttributes for Customer_Address entity. These fields are correctly displaying in checkout. For the customer address edit view I have overwritten the template of the customer_address_edit block.

In both cases the Post data is not saved into the database. No errors given, not system.log neither exception.log or any exception.

In Magento\Customer\Controller\AddressMagento\Customer\Controller\Address\FormPost seem to be some operations that will merge data columns of the customer_address_entity with extensions_attributes or custom_attributes. Some debugging shows me, that my extensions_attributes are recognized here.

But somehow the data get lost on its way to the database. To sum up my little module: I did mostly the same things as Bartlomiejsz in the of 27.Feb comment on this post: https://github.com/magento/magento2/issues/6575

asked Nov 30, 2017 at 1:42

1 Answer 1

0

Use this InstallData.php script to add attribute for customer_address

$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute('customer_address', 'suburb', [
 'label' => 'Suburb',
 'input' => 'text',
 'type' => 'varchar',
 'source' => '',
 'required' => false,
 'position' => 70,
 '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', 'suburb')
 ->addData(['used_in_forms' => [
 'adminhtml_customer_address',
 'customer_address_edit',
 'customer_register_address'
 ]]);
$attribute->save();
$salesSetup = $this->salesSetupFactory->create(['setup' => $setup]);
$salesSetup->addAttribute('order_address', 'suburb', ['type' => 'text']);
$quoteSetup = $this->quoteSetupFactory->create(['setup' => $setup]);
$quoteSetup->addAttribute('quote_address', 'suburb', ['type' => 'text']);
}
$setup->endSetup();

Create fieldset.xml in etc folder:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
 <scope id="global">
 <fieldset id="sales_convert_quote_address">
 <field name="suburb">
 <aspect name="to_order_address" />
 </field>
 </fieldset>
 </scope>
</config>

Override Magento\Sales\Model\Order\Address to set getter/setter methods:

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace VendorName\ModuleName\Model\Order;
use Magento\Customer\Api\Data\RegionInterfaceFactory;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Model\AbstractModel;
use Magento\Customer\Model\Address\AddressModelInterface;
/**
 * Sales order address model
 *
 * @method \Magento\Sales\Model\ResourceModel\Order\Address _getResource()
 * @method \Magento\Sales\Model\ResourceModel\Order\Address getResource()
 * @method \Magento\Customer\Api\Data\AddressInterface getCustomerAddressData()
 * @method Address setCustomerAddressData(\Magento\Customer\Api\Data\AddressInterface $value)
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 * @SuppressWarnings(PHPMD.ExcessivePublicCount)
 */
class Address extends \Magento\Sales\Model\Order\Address
{
 public function getSuburb($suburb)
 {
 return $this->getData('suburb', $suburb);
 }
 public function setSuburb($suburb)
 {
 return $this->setData('suburb', $suburb);
 }
}

di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <preference for="Magento\Sales\Model\Order\Address" type="<<VendorName>\<ModuleName>\Model\Order\Address"/>
</config>

Note: You have to place your attribute into checkout_index_index.xml or checkout onepage xml file.

Feel Free to ask!

answered Nov 30, 2017 at 5:00
4
  • Thanks for your suggestion, unfortunately it is not working yet. What do you mean by your note? Does it mean, even if I added my attribtues through "addAttribute in the Install Model and added in the fieldset.xml I still have to add it in the checkout_index_index.xml? Commented Dec 1, 2017 at 0:41
  • Yes you have to add the attribute in address form Commented Dec 1, 2017 at 4:47
  • I currently try to save the data that comes from my custom_attributes in customer_address edit view. The data is successfully passed through the objects, which I can see in Customer Address Model updateData method. But the data is not saved into DB. I think my attributes are not setup correctly. What items do I have to put Into eav value for used_in_forms to save the data in this view? Commented Dec 1, 2017 at 11:51
  • You can create an attribute as ''system' => true,` Commented Dec 1, 2017 at 11:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.