0

I am using Dynamic Rows in a module that saves data to the core_config_data table like this:

{"_1564763325005_5":{"addresses":"123 My Street, Bldg. 1, Reno, NV 85234"},"_1566830530708_708":{"addresses":"2123 My Street 2, Bldg 2, Reno, NV, 12345"}}

I am using this to take values and populate a dropdown box in a form that was built for checkout:

use Magento\Framework\Stdlib\ArrayManager;

class Options
{
    protected $arrayManager;

    public function __construct( ArrayManager $arrayManager )
    {
        $this->arrayManager = $arrayManager;
    }

    public function afterProcess(
        \Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array $jsLayout
    ) {
        $checkoutBuildingAddressOptionsPath = $this->arrayManager->findPath(
                'checkout_building_address',
                $jsLayout
            ).'/options';
        return $this->arrayManager->set(
            $checkoutBuildingAddressOptionsPath,
            $jsLayout,
            $this->getCustomOptions()
        );
    }

    /**
     * @return array
     */

    protected function getCustomOptions(): array
    {
        // TODO:: get it from somewhere
        $options = [
            [
                'value' => '1',
                'label' => 'This is the first option',
            ],
            [
                'value' => '2',
                'label' => 'Opt 2',
            ],
            [
                'value' => '3',
                'label' => 'Opt 3',
            ],
            [
                'value' => '4',
                'label' => 'Opt 4',
            ],
        ];
        return $options;
    }
}

I am having a hard time getting the data from the table into the $options array. How do I do this?

Thanks!

1

Here is what made it work:

protected function getCustomOptions(): array
    {
      // used $json as a test to see if it works
      $json = "{\"_1564763325005_5\":{\"addresses\":\"123 My Street, Bldg. 1, Palm Springs, CA 92234\"},\"_1566830530708_708\":{\"addresses\":\"2123 My Street, Bldg 23, Reno, NV, 12345\"}}";
      $arrayResult = json_decode($json, true);
      $options = [];

      foreach ($arrayResult as $item) {
        $options[] = [
        'value' => $item['addresses'],
        'label' => $item['addresses']
        ];
      }

      return $options;
    }

Now my options are populated from the core_config_data table from items created using dynamic rows.

| improve this answer | |

Your Answer

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

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