1

I created a custom field for customer:

<?php
declare(strict_types=1);

namespace Vendor\MyModule\Setup\Patch\Data;

use Magento\Customer\Model\Customer; 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 AddCustomAttributeCustomerAttribute implements DataPatchInterface, PatchRevertableInterface {

/**
 * @var ModuleDataSetupInterface
 */
private $moduleDataSetup;
/**
 * @var CustomerSetup
 */
private $customerSetupFactory;
/**
 * @var SetFactory
 */
private $attributeSetFactory;

/**
 * Constructor
 *
 * @param ModuleDataSetupInterface $moduleDataSetup
 * @param CustomerSetupFactory $customerSetupFactory
 * @param SetFactory $attributeSetFactory
 */
public function __construct(
    ModuleDataSetupInterface $moduleDataSetup,
    CustomerSetupFactory $customerSetupFactory,
    SetFactory $attributeSetFactory
) {
    $this-&gt;moduleDataSetup = $moduleDataSetup;
    $this-&gt;customerSetupFactory = $customerSetupFactory;
    $this-&gt;attributeSetFactory = $attributeSetFactory;
}

/**
 * {@inheritdoc}
 */
public function apply()
{
    $this-&gt;moduleDataSetup-&gt;getConnection()-&gt;startSetup();
    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this-&gt;customerSetupFactory-&gt;create(['setup' =&gt; $this-&gt;moduleDataSetup]);
    $customerEntity = $customerSetup-&gt;getEavConfig()-&gt;getEntityType(Customer::ENTITY);
    $attributeSetId = $customerEntity-&gt;getDefaultAttributeSetId();

    /** @var $attributeSet Set */
    $attributeSet = $this-&gt;attributeSetFactory-&gt;create();
    $attributeGroupId = $attributeSet-&gt;getDefaultGroupId($attributeSetId);

    $customerSetup-&gt;addAttribute(
        Customer::ENTITY,
        'custom_attribute',
        [
            'label' =&gt; 'My Custom Attribute',
            'input' =&gt; 'text',
            'type' =&gt; 'varchar',
            'source' =&gt; '',
            'required' =&gt; false,
            'position' =&gt; 333,
            'visible' =&gt; true,
            'system' =&gt; false,
            'is_used_in_grid' =&gt; true,
            'is_visible_in_grid' =&gt; true,
            'is_filterable_in_grid' =&gt; true,
            'is_searchable_in_grid' =&gt; false,
            'backend' =&gt; ''
        ]
    );

    $attribute = $customerSetup-&gt;getEavConfig()-&gt;getAttribute(Customer::ENTITY, 'custom_attribute');
    $attribute-&gt;addData([
        'used_in_forms' =&gt; [
             'adminhtml_customer',
             'adminhtml_checkout',
             'customer_account_create',
             'customer_account_edit'
        ]
    ]);
    $attribute-&gt;addData([
        'attribute_set_id' =&gt; $attributeSetId,
        'attribute_group_id' =&gt; $attributeGroupId

    ]);
    $attribute-&gt;save();

    $this-&gt;moduleDataSetup-&gt;getConnection()-&gt;endSetup();
}

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

    $this-&gt;moduleDataSetup-&gt;getConnection()-&gt;endSetup();
}

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

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

    ];
}

}

It show in the admin, I can edit... all work perfect!

But now I need add this field to Checkout.

I tried to updated the checkout_index_index.xml with this content:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.root">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="checkout" xsi:type="array">
                            <item name="children" xsi:type="array">
                                <item name="steps" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="shipping-step" xsi:type="array">
                                            <item name="children" xsi:type="array">
                                                <item name="shippingAddress" xsi:type="array">
                                                    <item name="children" xsi:type="array">
                                                        <item name="shipping-address-fieldset" xsi:type="array">
                                                            <item name="children" xsi:type="array">
                                                                <item name="custom_attribute" xsi:type="array">
                                                                    <item name="component" xsi:type="string">Magento_Ui/js/form/element/abstract</item>
                                                                    <item name="config" xsi:type="array">
                                                                        <item name="customScope" xsi:type="string">shippingAddress</item>
                                                                        <item name="template" xsi:type="string">ui/form/field</item>
                                                                        <item name="elementTmpl" xsi:type="string">ui/form/element/input</item>
                                                                    </item>
                                                                    <item name="provider" xsi:type="string">checkoutProvider</item>
                                                                    <item name="dataScope" xsi:type="string">shippingAddress.custom_attribute</item>
                                                                    <item name="label" xsi:type="string">My Custom Attribute</item>
                                                                    <item name="sortOrder" xsi:type="string">1</item>
                                                                    <item name="validation" xsi:type="array">
                                                                        <item name="required-entry" xsi:type="string">true</item>
                                                                    </item>
                                                                </item>
                                                        &lt;/item&gt;
                                                    &lt;/item&gt;
                                                &lt;/item&gt;
                                            &lt;/item&gt;
                                        &lt;/item&gt;
                                    &lt;/item&gt;
                                &lt;/item&gt;
                            &lt;/item&gt;
                        &lt;/item&gt;
                    &lt;/item&gt;
                &lt;/item&gt;
            &lt;/argument&gt;
        &lt;/arguments&gt;
    &lt;/referenceBlock&gt;
&lt;/body&gt;

</page>

But it doesnt show.

Thanks!

carlos
  • 37
  • 3

0 Answers0