0

I built a Magento 2 custom module for shipping that has a select drop down field for users to choose an option. I want to be able to set those options in the admin for the module using the dynamic row system. I have the form built and working. This is the section for the select box using:

frontend>layout>checkout_index_index.xml

<item name="checkout_building_address" xsi:type="array">
        <item name="component" xsi:type="string">Magento_Ui/js/form/element/select</item>
        <item name="config" xsi:type="array">
            <item name="customScope" xsi:type="string">customCheckoutForm</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="options" xsi:type="array">
            <item name="0" xsi:type="array">
                <item name="label" xsi:type="string">Please select value</item>
                <item name="value" xsi:type="string"></item>
            </item>
            <item name="1" xsi:type="array">
                <item name="label" xsi:type="string">Option 1</item>
                <item name="value" xsi:type="string">value_1</item>
            </item>
            <item name="2" xsi:type="array">
                <item name="label" xsi:type="string">Option 2</item>
                <item name="value" xsi:type="string">value_2</item>
            </item>
        </item>
        <item name="value" xsi:type="string">Please Select Value</item>
        <item name="provider" xsi:type="string">checkoutProvider</item>
        <item name="dataScope" xsi:type="string">customCheckoutForm.checkout_building_address</item>
        <item name="label" xsi:type="string">Select Building Address</item>
        <item name="sortOrder" xsi:type="string">2</item>
    </item>

I also followed the Mage docs for getting the dynamic rows in the admin. That is working fine and saves the information to the core_config_data table as a serialized array.

{"_1564590939871_871":{"addresses":"123 Amy Street Any Town, CA 92234"},"_1564593462830_830":{"addresses":"1475 Main St. Any Town, CA 90210"}}

I cannot figure out how to get that data to populate the select box in my form.

Thanks in advance for the help!

***** UPDATE ***** I am now using this in Helper>Data.php

public function getJsonConfigValue($configPath, $store = null)
    {
        $value = $this->getJsonConfigValue($configPath, $store);

        if (empty($value)) return false;

        if ($this->isSerialized($value)) {
            $unserializer = ObjectManager::getInstance()->get(\Magento\Framework\Unserialize\Unserialize::class);
        } else {
            $unserializer = ObjectManager::getInstance()->get(\Magento\Framework\Serialize\Serializer\Json::class);
        }

        return $unserializer->unserialize($value);
    }

and I can see that data in a template using:

public function getJsonSerialized()
    {   
        $arrayItems = $this->helper->getJsonConfigValue('carriers/dynamic/addresses/address_list');

        return json_encode($arrayItems);
    }

And this in a template file view>frontend>templates>array.phtml

<?php echo $block->getJsonSerialized(); ?>

And it appears like this on the page:

{"_1564763325005_5":{"addresses":"123 My Street, Bldg. 1, My Town, IA 00351"}}

How do I turn it into something that I can use in a form I built in a custom module to populate the select drop down field?

It this my problem? - return json_encode($arrayItems);

I can't find what I think I am looking for.

Thanks!

***** UPDATE 2 *****

This is part of the test XML to get the box on the page for the module.

view>frontend>layout>checkout_index_index.xml

<item name="checkout_building_address" xsi:type="array">
  <item name="component" xsi:type="string">Magento_Ui/js/form/element/select</item>
  <item name="config" xsi:type="array">

    <item name="customScope" xsi:type="string">customCheckoutForm</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="options" xsi:type="array">
    <item name="0" xsi:type="array">
      <item name="label" xsi:type="string">Please select value</item>
      <item name="value" xsi:type="string"></item>
    </item>
    <item name="1" xsi:type="array">
      <item name="label" xsi:type="string">Option 1</item>
      <item name="value" xsi:type="string">value_1</item>
    </item>
    <item name="2" xsi:type="array">
      <item name="label" xsi:type="string">Option 2</item>
      <item name="value" xsi:type="string">value_2</item>
    </item>
  </item>
  <item name="value" xsi:type="string">Please Select Value</item>
  <item name="provider" xsi:type="string">checkoutProvider</item>
  <item name="dataScope" xsi:type="string">customCheckoutForm.checkout_building_address</item>
  <item name="label" xsi:type="string">Select Building Address</item>
  <item name="sortOrder" xsi:type="string">2</item>
</item>

I used Api>Data>CustomFieldsInterface.php to get the data from XML

interface CustomFieldsInterface
{
    const CHECKOUT_BUYER_NAME = 'checkout_buyer_name';
    const CHECKOUT_BUYER_EMAIL = 'checkout_buyer_email';
    const CHECKOUT_COMMENT = 'checkout_comment';
    const CHECKOUT_BUILDING_ADDRESS = 'checkout_building_address';

    /**
     * Get checkout buyer name
     *
     * @return string|null
     */
    public function getCheckoutBuyerName();

    /**
     * Get checkout buyer email
     *
     * @return string|null
     */
    public function getCheckoutBuyerEmail();

    /**
     * Get checkout comment
     *
     * @return string|null
     */
    public function getCheckoutComment();

    /**
     * Get checkout building address
     *
     * @return string|null
     */
    public function getCheckoutBuildingAddress();

    /**
     * Set checkout buyer name
     *
     * @param string|null $checkoutBuyerName Buyer name
     *
     * @return CustomFieldsInterface
     */
    public function setCheckoutBuyerName(string $checkoutBuyerName = null);

    /**
     * Set checkout buyer email
     *
     * @param string|null $checkoutBuyerEmail Buyer email
     *
     * @return CustomFieldsInterface
     */
    public function setCheckoutBuyerEmail(string $checkoutBuyerEmail = null);

    /**
     * Set checkout comment
     *
     * @param string|null $comment Comment
     *
     * @return CustomFieldsInterface
     */
    public function setCheckoutComment(string $comment = null);

    /**
     * Set checkout building address
     *
     * @param string|null $checkoutBuildingAddress building Address
     *
     * @return CustomFieldsInterface
     */
    public function setCheckoutBuildingAddress(string $checkoutBuildingAddress = null);
}

From there I get it into to show in the frontend, order and the admin. Instead of using the dummy values I used in the XML file I need to replace it with the data from the array.

  • Get this data and decode json, then assign this using layoutprocessor – Sohel Rana Aug 2 at 1:44
  • @SohelRana - I made an update but I still don't totally have it. I must be not reading something right or missing it in the docs somewhere. Do you have more thoughts? – stanhook Aug 3 at 17:59
  • Where you want to show this in checkout page? – Sohel Rana Aug 4 at 9:32
  • @SohelRana - I made an update above. I am not using layoutProcessor in this case. i was working on a version of this module using one and I couldn't get the data in the admin. magento.stackexchange.com/questions/283830/… Hopefully that helps. – stanhook Aug 5 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.