3

i already know how to add new field to shipping address form in checkout, but i need to modify the behavior of some fields. For example:

  • there is a checkbox "Deliver to Packing station";
  • if checkbox is selected, "street" fields becomes hidden and not required, but 2 new fields ("Packing station #" and "Post #") becomes visible and required.

I completely stuck with that. I'm not very familiar with knockout, just have read some lessons on it. Any help/ideas? (except "go learn knockout.js" :))

3

1 Answer 1

0

Found solution, here is an example:

define([
    'Magento_Ui/js/form/element/single-checkbox'
], function (AbstractField) {
    'use strict';

    return AbstractField.extend({
        defaults: {
            modules: {
                packing_station_nr: '${ $.parentName }.packing_station_nr',
                post_nr: '${ $.parentName }.post_nr'
            }
        },

        onCheckedChanged: function () {
            this.packing_station_nr().visible(this.checked());
            this.post_nr().visible(this.checked());
            this._super()
        }


    });
});

P.S. this code only toggles fields visibility. Make fields required by default. If field is not visible, validator skips it's validation.

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.