1

I have a problem with the prefix field.

I have set it to be optional and I added two values in "Prefix Dropdown Options": Mr;Mrs.

Everything it's correct except from the first option of the generated select, which is empty.

Here is the code:

<div class="select select__wrapper">
  <select class="select" data-bind="
    attr: {
        name: inputName,
        id: uid,
        disabled: disabled,
        'aria-describedby': getDescriptionId(),
        'aria-required': required,
        'aria-invalid': error() ? true : 'false',
        placeholder: placeholder
    },
    hasFocus: focused,
    optgroup: options,
    value: value,
    optionsCaption: caption,
    optionsValue: 'value',
    optionsText: 'label',
    optionsAfterRender: function(option, item) {
        if (item &amp;&amp; item.disabled) {
            ko.applyBindingsToNode(option, {attr: {disabled: true}}, item);
        }
    }" name="prefix" id="SO7RWP3" aria-invalid="false">
    <option data-title="" value=" "></option>
    <option data-title="Mr" value="Mr">Mr</option>
    <option data-title="Mrs" value="Mrs">Mrs</option>
  </select>
</div>

Any ideas on how to add a translation for the first option like "Select a prefix"? Thanks

2 Answers 2

2

I am not sure that I got what You want, but it seems that its enough to change

<option data-title="" value=" "></option>

to

<option data-title="" value=" ">Select a prefix</option>

.

EDIT:
In that case look into:

src/vendor/magento/module-customer/Model/Options.php

there is function getNameSuffixOptions that calls prepareNamePrefixSuffixOptions there is :

$result = array_merge([' ' => ' '], $result);

that create empty value.
Override getNameSuffixOptions function or use di after, to change this value to Select a prefix.

6
  • The problem is that the select is automatically created somehow..I add the dropdown values from the admin panel... Commented May 12, 2020 at 13:21
  • 1
    i just edit my post Commented May 12, 2020 at 13:57
  • That's great! In order to override that function can I copy/paste the entire file in app/frontend/_THEME_/Default/Magento_Customer/Model/Options.php and make my changes there? Commented May 13, 2020 at 9:23
  • 1
    noooo. Use di.xml. Its like basic magento stuff devdocs.magento.com/guides/v2.3/extension-dev-guide/build/… or devdocs.magento.com/guides/v2.3/extension-dev-guide/… Commented May 13, 2020 at 11:01
  • I will have to investigate on it but you helped me getting in the right path! Thanks Commented May 13, 2020 at 12:49
0

Applying for checkout file is in deferent module

Override using preference in di.xml and

/vendor/magento/module-customer-custom-attributes/Model/Options.php

apply logic in line 142-150

$i=0; 
foreach ($options as $value) {
        $value = $this->escaper->escapeHtml(trim($value));
        if($i===0) {
            $result[' '] = ' ';
        }
        $result[$value] = $value;
        $i++;
    }

It works fine for me!!

Happy Coding!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.