0

I have a custom drop down field on my checkout on the shipping address and am having a difficult time making a default value. I have tried creating a template, specifying a default in a custom layout processor file, as well as trying to adjust through after render data-bind. I can’t seem to get it working and am sure I’mmissing something simple. Help or pointers are very much appreciated.

2 Answers 2

0
<?php 
namespace Vendor\CheckoutAdditionalField\Block\Checkout;




class LayoutProcessor
{
    /**
     * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
     * @param array $jsLayout
     * @return array
     */
    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array  $jsLayout
    ) {
        
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
        ['shippingAddress']['children']['shipping-address-fieldset']['children']['drop_down'] = [
            'component' => 'Magento_Ui/js/form/element/select',
            'config' => [
                'customScope' => 'shippingAddress',
                'template' => 'ui/form/field',
                'elementTmpl' => 'ui/form/element/select',
                'id' => 'drop-down',
            ],
            'dataScope' => 'shippingAddress.drop_down',
            'label' => 'Drop Down',
            'provider' => 'checkoutProvider',
            'visible' => true,
            'validation' => [],
            'sortOrder' => 251,
            'id' => 'drop-down',
            'options' => [
                [
                    'value' => '',
                    'label' => 'Please Select',
                ],
                [
                    'value' => '1',
                    'label' => 'First Option',
                ],
                [
                    'value' => '2',
                    'label' => 'Second option',
                ]
            ],
            'value' => '2' // value field is used to set a default value of the attribute
        ];

        return $jsLayout;
    }
}
0

Your answer is great if we weren't extending \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource and adjusting getAllOptions(). The value and labels are defined here. The answer has been in my face the entire time and is in that definition, it seems to be literally the order and the first value we have defined is the default. I think I have it sorted now. Thank you for the help.

Your Answer

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

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