Skip to content

[Issue] Use static const in Magento\Customer\Model\Address\Config #39135

Open
@m2-assistant

Description

@m2-assistant

This issue is automatically created based on existing pull request: #39123: Use static const in Magento\Customer\Model\Address\Config


Description (*)

This PR adds the possibility to create a new configuration from a custom path and create a class that extends from Magento\Customer\Model\Address\Config without having to duplicate the function Magento\Customer\Model\Address\Config::getFormats()

use Magento\Customer\Model\Address\Config;

class ClickAndCollectConfig extends Config
{
    public const XML_PATH_ADDRESS_TEMPLATE = 'click_and_collect/address_templates/';
}

Capture d’écran du 2024-08-29 10-16-48

Workaround

Today, the only solution to come to do is to duplicate the getFormats function ...

use Magento\Customer\Model\Address\Config;

class ClickAndCollectConfig extends Config
{
    public const XML_PATH_ADDRESS_TEMPLATE = 'click_and_collect/address_templates/';

    /**
     * @inheritDoc
     */
    public function getFormats(): ?array
    {
        $store = $this->getStore();
        $storeId = $store->getId();

        if (!isset($this->_types[$storeId])) {
            $this->_types[$storeId] = [];
            foreach ($this->get() as $typeCode => $typeConfig) {
                $path = sprintf('%s%s', self::XML_PATH_ADDRESS_TEMPLATE, $typeCode);
                $type = new DataObject();
                if (isset($typeConfig['escapeHtml'])) {
                    $escapeHtml = $typeConfig['escapeHtml'] == 'true' || $typeConfig['escapeHtml'] == '1';
                } else {
                    $escapeHtml = false;
                }

                $format = $this->_scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeId);
                $type->setData('code', $typeCode)
                    ->setData('title', (string)$typeConfig['title'])
                    ->setData('default_format', $format)
                    ->setData('escape_html', $escapeHtml);

                $renderer = isset($typeConfig['renderer']) ? (string)$typeConfig['renderer'] : null;
                if (!$renderer) {
                    $renderer = self::DEFAULT_ADDRESS_RENDERER;
                }

                // @phpstan-ignore-next-line
                $type->setRenderer($this->_addressHelper->getRenderer($renderer)->setType($type));
                $this->_types[$storeId][] = $type;
            }
        }

        return $this->_types[$storeId];
    }
}

Related Pull Requests

None

Fixed Issues (if relevant)

None

Manual testing scenarios (*)

  • Create new class extend Magento\Customer\Model\Address\Config
  • Setup XML_PATH_ADDRESS_TEMPLATE with custom value

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: FrameworkComponent: BackendIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Progress: PR in progressReported on 2.4.xIndicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions