4

I'm setting up a Multi-store in English, French and German. I'd like to enable Name Prefix as an optional field but Customer Configuration > Name and Address Options is only editable at the website level. So I cannot translate Mr, Mrs etc to Herr, Frau or Mr, Mme.

I saw a solution to a similar problem in M1 here but, being an average store owner, I don't know to adapt it to M2.

Has anyone found a way around this ? I can't be the first to run into this problem.

1
2

This is a Magento issue i know happens until 2.2.7 at least

you need to override using a custom module

di.xml:

preference for="Magento\Customer\Model\Options" type="Vendor\Module\Model\Options"

Class:

namespace Vendor\Module\Model;

use Magento\Config\Model\Config\Source\Nooptreq as NooptreqSource;

class Options extends \Magento\Customer\Model\Options {

    public function getNamePrefixOptions($store = null)
    {
        return $this->prepareNamePrefixSuffixOptions(
            $this->addressHelper->getConfig('prefix_options', $store),
            $this->addressHelper->getConfig('prefix_show', $store) == NooptreqSource::VALUE_OPTIONAL
        );
    }

    /**
     * Retrieve name suffix dropdown options
     *
     * @param null $store
     * @return array|bool
     */
    public function getNameSuffixOptions($store = null)
    {
        return $this->prepareNamePrefixSuffixOptions(
            $this->addressHelper->getConfig('suffix_options', $store),
            $this->addressHelper->getConfig('suffix_show', $store) == NooptreqSource::VALUE_OPTIONAL
        );
    }

    private function prepareNamePrefixSuffixOptions($options, $isOptional = false)
    {
        $options = trim($options);
        if (empty($options)) {
            return false;
        }
        $result = [];
        $options = explode(';', $options);
        foreach ($options as $value) {
            $value = $this->escaper->escapeHtml(trim($value));
            $result[__($value)] = __($value);
        }
        if ($isOptional && trim(current($options))) {
            $result = array_merge([' ' => ' '], $result);
        }

        return $result;
    }

}

the difference: original: $result[$value] = $value; fixed: $result[$value] = __($value);

3
  • ...it has to be: $result[__($value)->__toString()] = __($value);
    – michael
    May 7 '19 at 14:13
  • Solution is ugly, wondering if somebody makes a core - Pull Request -- but this is non trivial
    – Alex
    May 10 '19 at 14:43
  • 1
    is this still an issue ?
    – medmek
    Feb 11 '20 at 19:43
0

To translate name prefix on checkout page in Magento2, you only need some admin work. You can check this link to know how to do this.

Please write in comment if it works or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.