I am tring to add a customer custom attributes such as VAT.
So I will not use the address structure but account structure.
And i want to display the field during the payment and not during the first step (shipping info).
But the result is
This is the code
SetupInstallData.php
namespace VendorItalianFiscalAttributesSetup;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoCustomerModelCustomer;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoEavModelEntityAttributeSet as AttributeSet;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
private $_eavSetupFactory;
private $_attributeRepository;
/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
public function __construct(
MagentoEavSetupEavSetupFactory $eavSetupFactory,
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory,
MagentoEavModelAttributeRepository $attributeRepository
)
{
$this->_eavSetupFactory = $eavSetupFactory; // questa classe serve per costruire un attributo (prodotti , indirizzi, account ...)
$this->customerSetupFactory = $customerSetupFactory;
$this->_attributeRepository = $attributeRepository;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* {@inheritdoc}
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->_eavSetupFactory->create(['setup' => $setup]);
$customerSetupFac = $this->customerSetupFactory->create(['setup' => $setup]);
$setup->startSetup();
$customerEntity = $customerSetupFac->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
/* customer type è per identificare la tipologia di cliente privato italiano o azienda italiana
* in modo da capire se rendere obbligatori i campi fiscali e quali.
* altro non li rende obbligatori
*/
//customer fields
$customerSetup->removeAttribute(Customer::ENTITY, 'customer_type');
$customerSetup->addAttribute(Customer::ENTITY, 'customer_type', [
'type' => 'text',
'label' => 'Type of customer',
'input' => 'multiselect',
'class' => 'tipo_cliente',
'frontend_class' => 'validate-tipo-cliente',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 01,
'position' => 01,
'system' => 0,
'backend' => 'MagentoEavModelEntityAttributeBackendArrayBackend',
'source' => 'VendorItalianFiscalAttributesModelEntityAttributeSourceTypeCustomer',
'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_STORE
]);
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$customer_number = $customerSetupFac->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_type');
$customer_number
->setData("used_in_forms", $used_in_forms)
->setData("attribute_set_id", $attributeSetId)
->setData("attribute_group_id", $attributeGroupId)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 77);
$customer_number->save();
$setup->endSetup();
}}
etcmodule.xml
< ?xml version="1.0" ?>
etcfrontenddi.xml
< ?xml version="1.0" encoding="UTF-8"?>
PluginCheckoutLayoutProcessor.php
namespace VendorItalianFiscalAttributesPluginCheckout;
class LayoutProcessor
{
/**
* @param MagentoCheckoutBlockCheckoutLayoutProcessor $subject
* @param array $jsLayout
* @return array
*/
public function afterProcess(
MagentoCheckoutBlockCheckoutLayoutProcessor $subject,
array $jsLayout
) {
$configuration = $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'];
foreach ($configuration as $paymentGroup => $groupConfig) {
if (isset($groupConfig['component']) AND $groupConfig['component'] === 'Magento_Checkout/js/view/billing-address') {
$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
['payment']['children']['payments-list']['children'][$paymentGroup]['children']['form-fields']['children']['customer_type'] = [
'component' => 'Magento_Ui/js/form/element/abstract',
'config' => [
'customScope' => 'billingAddress.custom_attributes',
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/input',
'options' => [],
'id' => 'customer_type'
],
'dataScope' => 'billingAddress.custom_attributes.customer_type',
'label' => 'Customer Type',
'provider' => 'checkoutProvider',
'visible' => true,
'validation' => [],
'sortOrder' => 1,
'id' => 'customer_type'
];
}
}
return $jsLayout;
}
}