Magento 2 Checkout - 在发货方式部分动态填充下拉列表

Magento 2 Checkout - dynamically populated dropdown in shippment method section

我正在编写自定义送货方式(商店取货)。

我在本教程中添加了一个额外的下拉菜单:https://zanetabaran.com/how-to-in-magento-2-how-to-add-additional-dropdown-with-options-based-on-selected-shipping-methods-in-the-checkout/

下拉列表中的值目前是静态的,来自我的模块中的 js 文件-> Pastebin

updateDropdownValues: function(method) {
        var valuesCollection = [];

        if(method['carrier_code'] == 'customshipping'){

            valuesCollection = [
                {
                    label: 'Store1',
                    value: 'Store1'
                },
                {
                    label: 'Store2',
                    value: 'Store2'
                },
                {
                    label: 'Store3',
                    value: 'Store3'
                }
            ];
        } else {
            valuesCollection = [];
        }

        self.updateDropdown(valuesCollection);
    },

下拉列表在 checkout_index_index.xml -> Pastebin

中定义
    <item name="shippingAdditional" xsi:type="array">
                                                        <item name="component" xsi:type="string">uiComponent</item>
                                                        <item name="displayArea" xsi:type="string">shippingAdditional</item>
                                                        <item name="children" xsi:type="array">
                                                            <item name="shipping-option-wrapper" xsi:type="array">
                                                                <!-- Component Magento_Checkout/js/view/additional-shipping-option is used as a wrapper for content -->
                                                                <item name="component" xsi:type="string">XXX_CustomShipping/js/view/additional-shipping-option</item>
                                                                <item name="provider" xsi:type="string">checkoutProvider</item>
                                                                <item name="sortOrder" xsi:type="string">0</item>
                                                                <item name="children" xsi:type="array">
                                                                    <item name="shipping-option" xsi:type="array">
                                                                        <!-- uiComponent is used as a wrapper for select (its template will render all children as a list) -->
                                                                        <item name="component" xsi:type="string">uiComponent</item>
                                                                        <!-- the following display area is used in template -->
                                                                        <item name="displayArea" xsi:type="string">additionalShippingOptionField</item>
                                                                        <item name="children" xsi:type="array">
                                                                            <item name="markt" xsi:type="array">
                                                                                <item name="component" xsi:type="string">XXX_CustomShipping/js/view/shipping-option-select</item>
                                                                                <item name="config" xsi:type="array">
                                                                                    <!--customScope is used to group elements within a single form (e.g. they can be validated separately)-->
                                                                                    <item name="customScope" xsi:type="string">shippingOptionSelect</item>
                                                                                    <item name="template" xsi:type="string">ui/form/field</item>
                                                                                    <item name="elementTmpl" xsi:type="string">ui/form/element/select</item>
                                                                                </item>
                                                                                <item name="dataScope" xsi:type="string">shippingOptionSelect.select_data</item>
                                                                                <item name="label" xsi:type="string" translate="true">Please choose a market</item>
                                                                                <item name="provider" xsi:type="string">checkoutProvider</item>
                                                                                <item name="visible" xsi:type="boolean">true</item>
                                                                                <item name="validation" xsi:type="array">
                                                                                    <item name="required-entry" xsi:type="boolean">true</item>
                                                                                    <item name="validate-no-empty" xsi:type="boolean">true</item>
                                                                                </item>
                                                                                <item name="sortOrder" xsi:type="number">0</item>
                                                                            </item>
                                                                        </item>
                                                                    </item>
                                                                </item>
                                                            </item>
                                                        </item>

如何从 class 中获取值到下拉列表中?现在,我只能访问引用 class 中的值。我需要访问我自己的(仅供参考:显示不同商店的不同可用性)

如果需要更多信息,请随时索取。提前谢谢你。

好的,我明白了。

我添加了一个索引控制器 <Your_Vendor>/<YourModule>/Controller/Options/index.php,并在 <Your_Vendor>/<YourModule>/etc/frontent/routes.xml 中声明了它,并且可以使用 ajax 获取值:

    updateDropdownValues: function(method) {
        var valuesCollection = [];

        if(method['carrier_code'] == 'customshipping'){



            $.ajax({
              url:"/<your_declared_route>/Optionen/index",
              contentType: "application/json",
              async:false,
              success:function (data) {
                valuesCollection = [];
                var wert=[];
                $.each(data, function (index, thevalue) {
                  wert=[];
                  wert["label"]=index;
                  wert["value"]=thevalue;
                  valuesCollection.push(wert);
                });


              },
              error: function (xhr, ajaxOptions, thrownError) {
                console.log("There has been an error retrieving the values from the database.");
              }
            });
        }

        else {
            valuesCollection = [];
        }

        self.updateDropdown(valuesCollection);
    },

我的routes.xml:

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
        <router id="standard">
            <route id="yourid" frontName="<your_declared_route>">
                <module name="<your_Vendor/Your_Modulname>" />
            </route>
        </router>
    </config>