1

I am trying to create a group of options please see attached image enter image description here

I have tried by adding single field but it's creating separate filed, i need similar solution either in magento1 or magento2.

  • Yes, it can be possiable. – Amit Bera Nov 10 '18 at 5:16
  • Could please help me ? is there any source for the same ? – Arunendra Nov 10 '18 at 5:18
1

To achieve this requirement, you have to create

1. frontend_model

This is a block which will render the field view and it will extend Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray

Example: Devamitbera\Stackexchange\Block\Adminhtml\Form\Field\Groupcodes.

2. backend_model

This is a model which saves that field value using JSON serialized and during this field value load using _afterLoad function, it converts field value to the array and during save using beforeSave() it converts the field to array from JSON.

Example:

<?php

namespace Devamitbera\Stackexchange\Model\System\Config\Backend;

/**
 * Backend for serialized array data
 */
class Groupcodes extends \Magento\Framework\App\Config\Value
{

    protected $ConvertverClass = null;

    /**
     * @param \Magento\Framework\Model\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
     * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
     * @param \Magento\CatalogInventory\Helper\Minsaleqty $catalogInventoryMinsaleqty
     * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
     * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\App\Config\ScopeConfigInterface $config,
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
        \ConvertverClass $ConvertverClass,
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        array $data = []
    ) {
        $this->ConvertverClass = $ConvertverClass;
        parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
    }

    /**
     * Process data after load
     *
     * @return void
     */
    protected function _afterLoad()
    {
        $value = $this->getValue();
        // This is class which Convert json to array
        $value = $this->ConvertverClass->functiontoConVertArrayFromSJson($value);
        $this->setValue($value);
    }

    /**
     * Prepare data before save
     *
     * @return void
     */
    public function beforeSave()
    {
        $value = $this->getValue();
    // This is class which Convert json to array
        $value = $this->ConvertverClass->functiontoConVertJsonFromArray($value);
        $this->setValue($value);
    }
}

At this case, you can use the following Minimum Qty Allowed in Shopping Cart which exits at admin>Store>Configuration>Catalog>Inventory> Product Stock Options>Minimum Qty Allowed in Shopping Cart

Reference:

system.xml : system.xml

Frontend Model class Magento\CatalogInventory\Block\Adminhtml\Form\Field\Minsaleqty;

Model Class Magento\CatalogInventory\Model\System\Config\Backend\Minsaleqty

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.