0

I'm working on one project and i wants some extra fields on cart page under estimate shipping and tax.

  1. Hide Country field
  2. Show State as "Province" Text
  3. Show City as "Canton" as a Dropdown (data will load from database).
  4. Show "District" custom field as Dropdown (data will load from database)
  5. Show Postal Code as Dropdown (data will load from database)

i've modified vendor/magento/module-checkout/Block/Cart/LayoutProcessor.php file

Original Code

$elements = [
            'city' => [
                'visible' => $this->isCityActive(),
                'formElement' => 'text',
                'label' => __('City'),
                'value' =>  null
            ],
            'country_id' => [
                'visible' => true,
                'formElement' => 'select',
                'label' => __('Country'),
                'options' => [],
                'value' => null
            ],
            'region_id' => [
                'visible' => true,
                'formElement' => 'select',
                'label' => __('State'),
                'options' => [],
                'value' => null
            ],
            'postcode' => [
                'visible' => true,
                'formElement' => 'text',
                'label' => __('Postcode'),
                'value' => null
            ]
        ];

After Modification

$elements = [
            'city' => [
                'visible' => true,
                'formElement' => 'select',
                'label' => __('cant贸n'),
                'value' =>  null
            ],
            'country_id' => [
                'visible' => true,
                'formElement' => 'select',
                'label' => __('Country'),
                'options' => [],
                'value' => null
            ],
            'region_id' => [
                'visible' => true,
                'formElement' => 'select',
                'label' => __('Provincia'),
                'options' => [],
                'value' => null
            ],
            'postcode' => [
                'visible' => true,
                'formElement' => 'select',
                'label' => __('Postal Code'),
                'value' => null
            ],
            'distro' => [
                'visible' => true,
                'formElement' => 'select',
                'label' => __('Distro'),
                'value' => null
            ]
        ];

this code isn't working I'm facing error

1 exception(s):
Exception #0 (Exception): Notice: Undefined index: sortOrder in /home/samarpan/mage.samarpanherbslife.com/vendor/magento/module-checkout/Block/Checkout/AttributeMerger.php on line 194

if i remove

            'distro' => [
                'visible' => true,
                'formElement' => 'select',
                'label' => __('Distro'),
                'value' => null
            ]

then it's working fine but City comes after postcode. so kindly please suggest or help me for

  1. How to load data from database to city field (Dropdown).
  2. How to add Custom field(Dropdown) and load data into it from database.
  3. How to move city above Postcode
  4. How to create, Manage and best way for Custom Tables, those link with another tables like, Country and State already linked. City Depends on State, District Depends on City, Postal Code Depends on District.

Thank you.

1 Answer 1

0

I had the same problem, reading your question gave me a facepalm moment - its literally telling us the issue in the error. We're adding a new item, but we haven't told M2 in which order to show it. This hopefully answers 2 and 3 for you :)

Adding a "sortOrder" field did the trick for me

'my_custom_field' => [
    'visible' => true,
    'formElement' => 'input',
    'label' => __('Whoop! It shows'),
    'value' => null,
    'sortOrder' => 1
]

The item is now showing #1 in the list

edit I needed to move my field to the bottom, setting 5 didn't work I had to do 5000 < might be less but 5000 stuck it at the bottom for me

I only needed a field, as you need a select - I found an options snippet from another answer - this should work for you:

'my_custom_field' => [
    'visible' => true,
    'formElement' => 'select',
    'label' => __('Whoop! It shows'),
    'options' => [['label' => __('Yes'), 'value' => '1'], ['label' => __('No'), 'value' => '0']],
    'value' => null,
    'sortOrder' => 1
]

And now I can see a select box #1 with my new options:

The select is now showing as #1

You could do a loop to build the 'options' from the database.

Your Answer

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

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