Magento 2 How to get serialized data and use in Dropdown Box
-
11-03-2021 - |
Вопрос
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!
Решение
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.
Не связан с magento.stackexchange