2

I have created dynamic rows in configuration, same in SS:

enter image description here

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

9
  • please add frontend model
    – fmsthird
    Apr 23, 2019 at 14:15
  • @magefms where to add frontend model? can you explain a bit
    – Ajwad Syed
    Apr 23, 2019 at 14:16
  • sorry, i mean your frontend model code please add to your post
    – fmsthird
    Apr 23, 2019 at 14:17
  • Check the update
    – Ajwad Syed
    Apr 23, 2019 at 14:21
  • I see, I suggest you use a phtml template file to display your dynamic row, that way you can manipulate well how data will be stored and in your frontend model block you can create a function to get value of the saved data.
    – fmsthird
    Apr 23, 2019 at 14:26

1 Answer 1

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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