M2 | How to get value of dynamic generated rows values created at system configuration into template?
-
27-02-2021 - |
Pergunta
I have created dynamic rows in configuration, same in SS:
When I save value and check in:
core_config_data
the value it is not what I am saving instead its like
"{"_1556026243400_400":{"productcost":"7"}}"
in the corresponding value column.
This is my system.xml field:
<group id="prices" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Voucher Prices Setting</label>
<field id="add_item" translate="label" sortOrder="15" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Dropdown Prices</label>
<frontend_model>Vendor\Module\Block\Adminhtml\Menu\Field\AdditionalItem</frontend_model>
<backend_model>Vendor\Module\Block\Adminhtml\Menu\Config\Backend\AdditionalItem</backend_model>
</field>
</group>
Update:
Here is frontend model:
namespace Vendor\Module\Block\Adminhtml\Menu\Field;
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
class AdditionalItem extends AbstractFieldArray
{
protected $_typeblockOptions;
protected $_cmsblockOptions;
/**
* {@inheritdoc}
*/
protected function _prepareToRender()
{
$this->addColumn(
'productcost',
[
'label' => __('Product Cost'),
'size' => '200px',
'class' => 'required-entry'
]
);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add');
}
}
Can somebody figure what is missing ? Thanks
Solução
You can create helper into your extension at following location to get the value.
app\code\Vendor\Extension\Helper\Data.php
<?php
namespace Vendor\Extension\Helper;
use Magento\Store\Model\ScopeInterface;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
const PRODUCTCOST = 'pme_giftcard/prices/add_item';
protected $_storeManager;
protected $serialize;
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Serialize\Serializer\Json $serialize)
{
$this->_storeManager = $storeManager;
$this->serialize = $serialize;
parent::__construct($context);
}
public function getStoreid()
{
return $this->_storeManager->getStore()->getId();
}
public function getProductCost()
{
$productcostconfig = $this->scopeConfig->getValue(self::PRODUCTCOST,ScopeInterface::SCOPE_STORE,$this->getStoreid());
if($productcostconfig == '' || $productcostconfig == null)
return;
$unserializedata = $this->serialize->unserialize($productcostconfig);
$productcostarray = array();
foreach($unserializedata as $key => $row)
{
$productcostarray[] = $row['productcost'];
}
return $productcostarray;
}
}
Now call the helper function getProductCost()
to get the value of that field.
This return the value into form of array.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange