magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.57k stars 9.32k forks source link

"select" custom customer address attribute does not render for new customer address #34950

Open markshust opened 2 years ago

markshust commented 2 years ago

Custom customer address EAV attributes of 'type' => 'int' and 'input' => 'select' do not render after a logged-in customer creates a new address at checkout.

Preconditions (*)

  1. Base install of Magento 2.4.3-p1

Steps to reproduce (*)

  1. Create a custom module with a new setup patch data script to create a custom customer address attribute, with 'type' => 'int' and 'input' => 'select'. Ensure 'system' => false so it's created as a "custom" attribute.

app/code/MarkShust/MyModule/registration.php

<?php declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'MarkShust_MyModule',
    __DIR__
);

app/code/MarkShust/MyModule/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MarkShust_MyModule">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

app/code/MarkShust/MyModule/Setup/Patch/Data/AddAddressClassificationAttribute.php

<?php declare(strict_types=1);

namespace MarkShust\MyModule\Setup\Patch\Data;

use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Eav\Model\Config as EavConfig;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Customer\Model\ResourceModel\Attribute as AttributeResourceModel;
use MarkShust\MyModule\Model\Config\Source\AddressClassification;
use Zend_Validate_Exception;

class AddAddressClassificationAttribute implements DataPatchInterface
{
    const ATTRIBUTE_CODE = 'address_classification';

    private AttributeResourceModel $attributeResourceModel;
    private EavConfig $eavConfig;
    private EavSetupFactory $eavSetupFactory;
    private ModuleDataSetupInterface $moduleDataSetup;

    public function __construct(
        AttributeResourceModel $attributeResourceModel,
        EavConfig $eavConfig,
        EavSetupFactory $eavSetupFactory,
        ModuleDataSetupInterface $moduleDataSetup
    ) {
        $this->attributeResourceModel = $attributeResourceModel;
        $this->eavConfig = $eavConfig;
        $this->eavSetupFactory = $eavSetupFactory;
        $this->moduleDataSetup = $moduleDataSetup;
    }

    /**
     * @return array
     */
    public static function getDependencies(): array
    {
        return [];
    }

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

    /**
     * @return $this
     * @throws LocalizedException
     * @throws AlreadyExistsException
     * @throws Zend_Validate_Exception
     */
    public function apply(): self
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute(
            AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
            self::ATTRIBUTE_CODE,
            [
                'type' => 'int',
                'label' => 'Address Classification',
                'input' => 'select',
                'source' => AddressClassification::class,
                'required' => true,
                'default' => 0,
                'system' => false,
                'position' => 150,
                'sort_order' => 150,
            ]
        );
        $attribute = $this->eavConfig->getAttribute(
            AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
            self::ATTRIBUTE_CODE
        );
        $attribute->setData('used_in_forms', [
            'adminhtml_customer_address',
            'customer_address_edit',
            'customer_register_address',
        ]);
        $this->attributeResourceModel->save($attribute);

        return $this;
    }
}

app/code/MarkShust/MyModule/Model/Config/Source/AddressClassification.php

<?php declare(strict_types=1);

namespace MarkShust\MyModule\Model\Config\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
use Magento\Eav\Model\Entity\Attribute\Source\SourceInterface;
use Magento\Framework\Data\OptionSourceInterface;

class AddressClassification extends AbstractSource implements OptionSourceInterface, SourceInterface
{
    public function getAllOptions(): array
    {
        return [
            [
                'value' => 0,
                'label' => 'Residential',
            ],
            [
                'value' => 1,
                'label' => 'Commercial',
            ],
        ];
    }
}
  1. Enable the module with bin/magento module:enable MarkShust_MyModule.
  2. Execute the setup patch data script by running bin/magento setup:upgrade.
  3. Go to frontend of the site and log in as a registered customer.
  4. Add a product to the cart and start checkout.
  5. At the Shipping step under Shipping Address, click New Address.
  6. Enter the new address info along with a value for the custom attribute, then click Ship Here.

Expected result (*)

  1. I expect to see the custom customer address attribute rendered just as the saved addresses do:

Screen Shot 2022-01-08 at 10 33 39 AM

  1. Note how saved addresses are created under a custom_attributes property. This differs from the data stored for the new customer address as explained in the Actual result info below. They are also stored as an object containing a label and value, as opposed to just an integer:

Screen Shot 2022-01-09 at 10 18 09 AM

  1. I'd expect the way the data assigned to the new customer address to be congruent with how data is stored for saved addresses.

Actual result (*)

  1. The new address is created, but the custom customer attribute does not display. Note how the attribute is stored as a child property directly on the newCustomerShippingAddress property, and as an integer (address_classification: 0).

Screen Shot 2022-01-08 at 10 33 10 AM


m2-assistant[bot] commented 2 years ago

Hi @markshust. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

markshust commented 2 years ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 years ago

Hi @markshust. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 2 years ago

Hi @markshust, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.

markshust commented 2 years ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 years ago

Hi @markshust. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 2 years ago

Hi @markshust, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.

markshust commented 2 years ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 years ago

Hi @markshust. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 2 years ago

Hi @markshust, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.

markshust commented 2 years ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 years ago

Hi @markshust. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 2 years ago

Hi @markshust, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.

markshust commented 2 years ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 years ago

Hi @markshust. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 2 years ago

Hi @markshust, unfortunately there is no ability to deploy Magento instance at the moment. Please try again later.

m2-assistant[bot] commented 2 years ago

Hi @engcom-Lima. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Lima commented 2 years ago

Hi @markshust ,

Thank you for reporting the issue.

We were able to reproduce this issue on 2.4-develop instance.

Steps followed to reproduce the issue :-

  1. Install fresh Magento 2.4-develop instance
  2. Create a custom module to add custom customer address attribute. Create following files for this step:-

app/code/MarkShust/MyModule/registration.php

<?php declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'MarkShust_MyModule',
    __DIR__
);

app/code/MarkShust/MyModule/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MarkShust_MyModule">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

app/code/MarkShust/MyModule/Setup/Patch/Data/AddAddressClassificationAttribute.php

<?php declare(strict_types=1);

namespace MarkShust\MyModule\Setup\Patch\Data;

use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Eav\Model\Config as EavConfig;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Customer\Model\ResourceModel\Attribute as AttributeResourceModel;
use MarkShust\MyModule\Model\Config\Source\AddressClassification;
use Zend_Validate_Exception;

class AddAddressClassificationAttribute implements DataPatchInterface
{
    const ATTRIBUTE_CODE = 'address_classification';

    private AttributeResourceModel $attributeResourceModel;
    private EavConfig $eavConfig;
    private EavSetupFactory $eavSetupFactory;
    private ModuleDataSetupInterface $moduleDataSetup;

    public function __construct(
        AttributeResourceModel $attributeResourceModel,
        EavConfig $eavConfig,
        EavSetupFactory $eavSetupFactory,
        ModuleDataSetupInterface $moduleDataSetup
    ) {
        $this->attributeResourceModel = $attributeResourceModel;
        $this->eavConfig = $eavConfig;
        $this->eavSetupFactory = $eavSetupFactory;
        $this->moduleDataSetup = $moduleDataSetup;
    }

    /**
     * @return array
     */
    public static function getDependencies(): array
    {
        return [];
    }

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

    /**
     * @return $this
     * @throws LocalizedException
     * @throws AlreadyExistsException
     * @throws Zend_Validate_Exception
     */
    public function apply(): self
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute(
            AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
            self::ATTRIBUTE_CODE,
            [
                'type' => 'int',
                'label' => 'Address Classification',
                'input' => 'select',
                'source' => AddressClassification::class,
                'required' => true,
                'default' => 0,
                'system' => false,
                'position' => 150,
                'sort_order' => 150,
            ]
        );
        $attribute = $this->eavConfig->getAttribute(
            AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
            self::ATTRIBUTE_CODE
        );
        $attribute->setData('used_in_forms', [
            'adminhtml_customer_address',
            'customer_address_edit',
            'customer_register_address',
        ]);
        $this->attributeResourceModel->save($attribute);

        return $this;
    }
}

app/code/MarkShust/MyModule/Model/Config/Source/AddressClassification.php

<?php declare(strict_types=1);

namespace MarkShust\MyModule\Model\Config\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
use Magento\Eav\Model\Entity\Attribute\Source\SourceInterface;
use Magento\Framework\Data\OptionSourceInterface;

class AddressClassification extends AbstractSource implements OptionSourceInterface, SourceInterface
{
    public function getAllOptions(): array
    {
        return [
            [
                'value' => 0,
                'label' => 'Residential',
            ],
            [
                'value' => 1,
                'label' => 'Commercial',
            ],
        ];
    }
}
  1. Enable the module bin/magento module:enable MarkShust_MyModule
  2. Install the data run bin/magento setup:upgrade
  3. Go to frontend of the site and log in as a registered customer.
  4. Add a product to the cart and start checkout.
  5. At the Shipping step under Shipping Address, click New Address.
  6. Enter the new address info along with a value for the custom attribute, then click Ship Here.

Expected Result: The custom address attribute should be rendered just as it is being rendered for previous addresses.

Actual Result: Custom address attribute is not rendered.

Screenshot 2022-01-31 at 5 19 12 PM

As we can see above, the attribute Residential is set for default address but not for newly added address. Based on this confirming the issue.

Thanks!

github-jira-sync-bot commented 2 years ago

:x: Something went wrong. Cannot create Jira issue.

engcom-Hotel commented 2 years ago

Hello @engcom-Lima,

Thanks for your input!

As per our discussion in the triage call, please try to reproduce the issue with the fresh Magento module.

Thanks

engcom-Lima commented 2 years ago

Hi @engcom-Hotel, I tried reproducing the issue with the fresh Magento 2.4-develop instance and the issue is still reproducible. I created the custom module and added the patch script following the documentation and as per the reproducible steps mentioned by @markshust I was able to reproduce the issue. 34950

Based on this confirming the issue!

Thanks.

github-jira-sync-bot commented 2 years ago

:x: You don't have permission to export this issue.

github-jira-sync-bot commented 2 years ago

:white_check_mark: Jira issue https://jira.corp.magento.com/browse/AC-2341 is successfully created for this GitHub issue.

m2-assistant[bot] commented 2 years ago

:white_check_mark: Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

engcom-Hotel commented 1 week ago

We have tried to reproduce the issue in the latest development branch i.e. 2.4-develop and the issue is still relevant. The added attribute is not visible in the checkout page:

We have selected Residential while adding new address: image

But it is not showing here: image

Also we are unable to checkout with address because the Address Classification field is marked as required: image

Hence reconfirming the issue and prioritize it as P1.

Thanks

engcom-Bravo commented 4 days ago

Hi @markshust,

The Magento core engineering team is working on the issue and may do further implementation to cover few more scenarios as needed. We will reach out to you if we need more information.

Thank you once again!