Documentation | NWICODE

Module My account

You can expandMy accountfields for your modules.

It is importantAccount::addFieldsuse the following hook frominit.php


Account::addFields(string $moduleName, array $fields, function $populateCallback, function $saveCallback);
"spacer",
                "key" => "mymodule_spacer",
            ],
            [
                "type" => "divider",
                "key" => "mymodule_divider",
                "label" => __("Divider #1"),
            ],
            [
                "type" => "text",
                "key" => "mobile_number",
                "label" => __("Mobile number"),
            ],
            [
                "type" => "textarea",
                "key" => "address",
                "rows" => "3",
                "label" => __("Address"),
            ],
            [
                "type" => "number",
                "key" => "age",
                "min" => "1",
                "max" => "100",
                "step" => "1",
                "label" => __("Age"),
            ]
        ],
        "populateMyFields",
        "saveMyFields"
    );
    return $payload;
}

Different types of fields

Type use
spacer
This is a 20px gap.
divider
This is the header separator, it usesitem item-divider item-divider-custom classes
text
A simple text input
TextArea Text area with custom rows
number
Input range withminmax andstep settings
select
Drop down selector withoptions: {["value": "single", "label": "Single"], ["value": "married", "label": "Married"]}

Examples of callbacks

$ PopulateCallback is called before the field is created, it is used to populate them with actual values and / or default.

Note: $ populateCallback should be pure function that is accessible from any location, it is recommended to declare it insideinit.php

(current application),
     *   "request" => (current request),
     *   "session" => (user session)
     * ]
     * @var $fields
     * [
     *    [
     *       "key" => "#Field_key#",
     *       "value" => "#Field_value", // defaults empty
     *    ],
     *    [...]
     * ]
     */

    $customer = $context["session"]->getCustomer();
    foreach ($fields as &$field) {
        switch ($field["key"]) {
            case "mobile_number":
                $field["value"] = $customer->getMobileNumber();
                break;
            case "address":
                $field["value"] = $customer->getAddress();
                break;
            case "age":
                $field["value"] = $customer->getAge();
                break;
        }
    }

    return $fields;
}

$ SaveCallback is called before the field is created, it is used to populate them with actual values and / or default.

Note: $ saveCallback should be pure function that is accessible from any location, it is recommended to declare it insideinit.php

(current application),
     *   "request" => (current request),
     *   "session" => (user session)
     * ]
     * @var $fields
     * [
     *    [
     *       "key" => "#Field_key#",
     *       "value" => "#Field_value", // defaults empty
     *    ],
     *    [...]
     * ]
     */

    $customer = $context["session"]->getCustomer();
    foreach ($fields as $field) {
        switch ($field["key"]) {
            case "mobile_number":
                $customer->setMobileNumber($field["value"]);
                break;
            case "address":
                $customer->setAddress($field["value"]);
                break;
            case "age":
                $customer->setAge($field["value"]);
                break;
        }
    }

    // Finally save the customer
    $customer->save();

    return $fields;
}



Есть вопросы — спрашивайте!

Наши специалисты помогут Вам, окажут бесплатную консультацию или запишут на приём

Documentation

Новости 1 - 20 of 47
First | Prev. | 1 2 3 | Next | Last