How to save a new address attribute in checkout - magento2

I am trying to save geolocation along with the customer address.
I have added Let & Lng using the install script
$customerSetup->addAttribute('customer_address', 'latitude', [
'type' => 'varchar',
'label' => 'Latitude',
'input' => 'text',
'required' => false,
'visible' => true,
'visible_on_front' => true,
'user_defined' => false,
'sort_order' => 43,
'position' => 43,
'system' => 0,
]);
$attributeLat = $customerSetup->getEavConfig()->getAttribute('customer_address', 'latitude')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address','customer_address'],
]);
$attributeLat->save();
//latitude - End
$customerSetup->addAttribute('customer_address', 'longitude', [
'type' => 'varchar',
'label' => 'Longitude',
'input' => 'text',
'required' => false,
'visible' => true,
'visible_on_front' => true,
'user_defined' => false,
'sort_order' => 43,
'position' => 43,
'system' => 0,
]);
$attributeLng = $customerSetup->getEavConfig()->getAttribute('customer_address', 'longitude')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address','customer_address'],
]);
$attributeLng->save();
//longitude - End}
I can update values in the admin backend without an any issue.
In the checkout page form fields for "latitude" & "longitude" are appearing. But values don't save along with the customer address.
I am using Magento CE 2.2.3

You need to add etc/extension_attributes.xml for Magento\Customer\Api\Data\AddressInterface:
<extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
<attribute code="longitude" type="string" />
</extension_attributes>
Add etc/fieldset.xml:
<fieldset id="sales_convert_quote_address">
<field name="longitude">
<aspect name="to_customer_address" />
<aspect name="to_order_address" />
</field>
</fieldset>
Add plugin in di.xml for Magento\Customer\Model\Address where you need to save your custom attribute in the beforeUpdateData function
<type name="Magento\Customer\Model\Address">
<plugin disabled="false" name="vendor_plugin_quote_model_address" sortOrder="10"
type="Vendor\Module\Plugin\Customer\Model\Address"/>
</type>
public function beforeUpdateData(
\Magento\Customer\Model\Address $subject,
\Magento\Customer\Api\Data\AddressInterface $address
)
And then you should be able to see your attribute saved on the Customer Address.

Related

How can I hide create new button in TYPO3 TCA type inline?

I would like to hide "create new" image button in case of TCA for field type is inline.
My code is below:
<pre>
'image' => array(
'label' => 'Image',
'config' => array(
'type' => 'inline',
'foreign_table' => 'sys_file_reference',
'foreign_field' => 'uid_foreign',
'foreign_sortby' => 'sorting_foreign',
'foreign_table_field' => 'tablenames',
'foreign_match_fields' => array(
'fieldname' => 'field_slide_image',
),
'foreign_label' => 'uid_local',
'foreign_selector' => 'uid_local',
'foreign_selector_fieldTcaOverride' => array(
'config' => array(
'appearance' => array(
'elementBrowserType' => 'file',
'elementBrowserAllowed' => $allowedFileExtensions
)
)
),
'filter' => array(
array(
'userFunc' => 'TYPO3\\CMS\\Core\\Resource\\Filter\\FileExtensionFilter->filterInlineChildren',
'parameters' => array(
'allowedFileExtensions' => $allowedFileExtensions,
'disallowedFileExtensions' => $disallowedFileExtensions
)
)
),
'appearance' => array(
'useSortable' => TRUE,
'headerThumbnail' => array(
'field' => 'uid_local',
'width' => '45',
'height' => '45c',
),
'showPossibleLocalizationRecords' => FALSE,
'showRemovedLocalizationRecords' => FALSE,
'showSynchronizationLink' => FALSE,
'showAllLocalizationLink' => FALSE,
'showPossibleRecordsSelector' => "hide",
'enabledControls' => array(
'info' => FALSE,
'new' => false,
'dragdrop' => TRUE,
'sort' => true,
'hide' => TRUE,
'delete' => TRUE,
'localize' => TRUE,
),
),
'behaviour' => array(
'localizationMode' => 'select',
'localizeChildrenAtParentLocalization' => TRUE,
),
),
)
</pre>
I have added this code 'new' => false, but still it is not working.
Found Solution :
I have found one solution https://forge.typo3.org/issues/71918
I hope this can help for other users.
Use the permission system of TYPO3 to only allow read access to the field for a certain user group.
Hide "New" Button in TCA with
['appearance']['enabledControls']['new'] = false
This works in 8.x only.
Found Solution :
I have found one solution https://forge.typo3.org/issues/71918
// Render the level links (create new record):
if ($config['appearance']['enabledControls']['new']) {
> $levelLinks = $this->getLevelInteractionLink('newRecord', $nameObject . '-' . $foreign_table, $config);
}
Afaik, the "new" button only becomes hidden if the "maxitems" limit is set and reached.

custom attribute displaying but is not saving in magento 2 admin customer creation form

i have created new customer attribute in my magento 2 environment
field is added the data in that filed is not saving.i am getting error as something went wrong while saving data.there is no good tutorials i could find to add new attribute.please help with this.
i have followed this code
https://magento.stackexchange.com/questions/128178/magento-2-add-custom-attribute-in-customer-registration-form
Customer Registration Custom attribute value create in admin and save
1. Text Field
2. Drop Down Field
3. Date Field
Using UpgradeSchema.php
<?php
namespace {CompanyName}\{ModuleName}\Setup;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
/* irrelevant */
#use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
/* irrelevant */
#use Magento\Framework\Setup\SchemaSetupInterface;
/* add this */
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class UpgradeData implements UpgradeDataInterface
{
private $customerSetupFactory;
public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '1.0.1', '<'))
{
// For Text field
$customerSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'attribute_title',
[
'type' => 'text',
'input' => 'text',
'label' => 'Attribute Title',
'required' => false,
'visible' => true,
'user_defined' => false,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]
);
$attribute_title = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'attribute_title')
->addData(
['used_in_forms' => ['adminhtml_customer']
]);
$attribute_title->save();
//Add field Drop Down for Yes/No
$customerSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'is_attribute',
[
'type' => 'int',
'input' => 'select',
'label' => 'Is Attribute',
'frontend' => '',
'default' => '1',
'class' => '',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'required' => false,
'visible' => true,
'user_defined' => false,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]
);
$is_attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_attribute')
->addData(
['used_in_forms' => ['adminhtml_customer']
]);
$is_attribute->save();
// For Date And Time field
$customerSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'custom_date',
[
'label' => 'Custom Date',
'type' => 'datetime',
'input' => 'date',
'frontend' => 'Magento\Eav\Model\Entity\Attribute\Frontend\Datetime',
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\Datetime',
'validate_rules' => '{"input_validation":"date"}',
'user_defined' => false,
'required' => false,
'visible' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]
);
$custom_date = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_date')
->addData(
['used_in_forms' => ['adminhtml_customer']
]);
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
$commenced_business->save();
}
}
}
Data save will not happen by itself you need to set data into attribute.
If its part of customer Interface
For example
$attribute = $customer->getCustomAttribute('client_dn');
if ($attribute)
{
$customer->setValue("hi");
}
if you are saving using customer object
$customer->setData('client_dn', 'Hi');

TYPO3 changing an extension with own extension

I'm trying to create a extension to modify fields in a different extension.
My extension needs to add and disable fields in fe_users over the TSConfig Page.
Ive looked over google how to do this with a own extension. But I didn't find anything usefull that I could work with.
(Edited)
The admin shouldn't be able to see these fields:
Company
Name
Middle name
Address
Zipcode
Land
Phone
Fax
www
Image
TSConfig
Bind a Domain
Redirect after login
Start
Stop
Record Type
These fields should be added
Customer (INT, not able to edit on display) Manditory
swissaxis_id (INT, Unique number) if possible only displayable and
not editing possibility
shop_rights (Textarea, No defined Value. The Rights will be saved
there serialised.)
fe_groups
These fields shouldn't be displayed to any Admin
Bind a domain
TSConfig
Redirect after login
Record Type
I'm thankfull for any Feedback possible.
Here's a link on how you add new fields to fe_users: https://docs.typo3.org/typo3cms/TCAReference/ExtendingTca/Index.html
https://docs.typo3.org/typo3cms/TCAReference/ExtendingTca/Examples/Index.html
I'll give you an example from an old, makeshift extension zusatzfelder of mine that modifies the "pages" table. It's really old, please verify if everything is current. You can also look at any other, "real" extension...
ext_emconf.php (maybe created by extension_builder)
<?php
########################################################################
# Extension Manager/Repository config file for ext "zusatzfelder".
#
# Auto generated 29-08-2011 15:33
#
# Manual updates:
# Only the data in the array - everything else is removed by next
# writing. "version" and "dependencies" must not be touched!
########################################################################
$EM_CONF[$_EXTKEY] = array(
'title' => 'Zusatzfelder',
'description' => '',
'category' => '',
'author' => '',
'author_email' => '',
'shy' => '',
'dependencies' => '',
'conflicts' => '',
'priority' => '',
'module' => '',
'state' => '',
'internal' => '',
'uploadfolder' => 0,
'createDirs' => '',
'modify_tables' => '',
'clearCacheOnLoad' => 0,
'lockType' => '',
'author_company' => '',
'version' => '0.0.0',
'constraints' => array(
'depends' => array(
),
'conflicts' => array(
),
'suggests' => array(
),
),
'_md5_values_when_last_written' => 'a:8:{s:9:"ChangeLog";s:4:"5b94";s:10:"README.txt";s:4:"ee2d";s:12:"ext_icon.gif";s:4:"1bdc";s:14:"ext_tables.php";s:4:"474a";s:14:"ext_tables.sql";s:4:"ead9";s:16:"locallang_db.xml";s:4:"7a92";s:19:"doc/wizard_form.dat";s:4:"0cba";s:20:"doc/wizard_form.html";s:4:"29e8";}',
);
?>
ext_tables.sql
CREATE TABLE pages (
tx_zusatzfelder_contentnav_title_addition tinytext,
tx_zusatzfelder_contentnav_title tinytext,
tx_zusatzfelder_contentnav_disable int(11) DEFAULT '0' NOT NULL,
);
ext_tables.php
<?php
if (!defined('TYPO3_MODE')) {
die ('Access denied.');
}
$tempColumns = array (
'tx_zusatzfelder_contentnav_title' => array (
'exclude' => 0,
'label' => 'LLL:EXT:zusatzfelder/locallang_db.xml:pages.tx_zusatzfelder_contentnav_title',
'config' => array (
'type' => 'input',
'size' => '30',
)
),
'tx_zusatzfelder_contentnav_title_addition' => array (
'exclude' => 0,
'label' => 'LLL:EXT:zusatzfelder/locallang_db.xml:pages.tx_zusatzfelder_contentnav_title_addition',
'config' => array (
'type' => 'input',
'size' => '30',
)
),
'tx_zusatzfelder_contentnav_disable' => array (
'exclude' => 0,
'label' => 'LLL:EXT:zusatzfelder/locallang_db.xml:pages.tx_zusatzfelder_contentnav_disable',
'config' => array (
'type' => 'check',
'default' => '0',
)
),
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('pages',$tempColumns,1);
// http://typo3-blog.net/tutorials/news/addtoalltcatypes.html
// PS: the "after:"... is for placement in the BE, stopped working last week...
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('pages','tx_zusatzfelder_contentnav_title;;;;1-1-1','','after:subtitle');
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('pages','tx_zusatzfelder_contentnav_title_addition;;;;1-1-1','','after:subtitle');
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes('pages','tx_zusatzfelder_contentnav_disable;;;;1-1-1','','after:subtitle');
?>
locallang_db.xml
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<T3locallang>
<meta type="array">
<type>database</type>
<description>Language labels for database tables/fields belonging to extension 'zusatzfelder'</description>
</meta>
<data type="array">
<languageKey index="default" type="array">
<label index="pages.tx_zusatzfelder_contentnav_title_addition">Untermenu: Vorlauf Titel (zB. "Mehr zur")</label>
<label index="pages.tx_zusatzfelder_contentnav_title">Untermenu: Titellink anderer Text (Standard: Seitentitel; Leerschlag: kein Titel)</label>
<label index="pages.tx_zusatzfelder_contentnav_disable">Untermenu ausblenden</label>
</languageKey>
</data>
</T3locallang>
That's all you need to add new fields – you don't even need the locallang if you just prefer to do 'label' => 'My untranslated Label', in ext_tables.php.

Magento "visible" config key not recognized in EAV custom attribute

I am using the getDefaultEntities() function which is run from my installer script. It mostly works, almost all of the attribute config keys reflect properly in the Attributes section of the admin. However the "visible," "visible_on_front" and some of the other properties do not work at all. My custom attribute is always set to not visible, not visible on front end. Can anyone spot what I am doing wrong?
class Ia_AdvancedShipping_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{
/**
* #return array
*/
public function getDefaultEntities()
{
return array(
'catalog_product' => array(
'entity_model' => 'catalog/product',
'attribute_model' => 'catalog/resource_eav_attribute',
'table' => 'catalog/product',
'additional_attribute_table' => 'catalog/eav_attribute',
'entity_attribute_collection' => 'catalog/product_attribute_collection',
'attributes' => array(
'iaadvancedshipping_profile' => array(
'group' => 'Advanced Shipping',
'label' => 'Shipping Profiles',
'type' => 'varchar',
'input' => 'select',
'source' => 'iaadvancedshipping/product_attribute_source_profiles',
'default' => '0',
'class' => '',
'backend' => '',
'frontend' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'visible_in_advanced_search' => false,
'unique' => false
),
)
)
);
}
}

How to pre-select a form radio item with Symfony 2?

I'm working on a language choice form:
$currentLocale = "en_US"; // This is indeed sent to the formType
$langs = array(
'fr_FR' => 'fr',
'en_US' => 'en'
);
$builder->add('language', 'language', array(
'choices' => $langs,
'expanded' => true,
'multiple' => false,
'required' => false,
'label' => false,
));
The HTML code looks like this (simplified):
<div id="languageForm_language">
<input type="radio" value="fr_FR">
<input type="radio" value="en_US">
</div>
How could I get the second item pre-selected, according to the $currentLocale value ?
In your $langs array you can specify key value pairs like this:
array(
0 => 'value1',
1 => 'value2'
)
Now, e.g. you want to preselect value2, you can set the data attribute to the key from value2:
$builder->add('language', 'choice', array(
'choices' => $langs,
'expanded' => true,
'multiple' => false,
'required' => false,
'label' => false,
'data' => 1
));
According to this, you can set your data attribute to your $currentLocale variable to preselect it. Your code should look like this:
$currentLocale = "en_US"; // This is indeed sent to the formType
$langs = array(
'fr_FR' => 'fr',
'en_US' => 'en'
);
$builder->add('language', 'choice', array(
'choices' => $langs,
'expanded' => true,
'multiple' => false,
'required' => false,
'label' => false,
'data' => $currentLocale
));
Note: the second parameter from the add() method should be choice not language.
If the form is used with a model object, just set the language on the object itself before passing it to the form:
$object->setLanguage($currentLocale);
$form = $this->createForm('some_form_type', $object);
Otherwise, set the data option to the default language key:
$builder->add('language', 'language', array(
'choices' => $langs,
'data' => $currentLocale,
// ...
));