I have need to add dynamic text field in cart price rule edit section. when click on Add button text field should be added. And save data in salesrule table.
When select from Apply Percent Discount: Buy Min N Products then only this text field should be display else hide it.
I have created one field in salesrule table for save data. Need to implement Add multiple text field in cart price rule using UI Components when click on Add button.
I have checked but we can do this thing in system.xml but I need to do in UI Components for cart price rule action section.
Admin Path:
Admin > Marketing > Promotions > Cart Price Rules > Edit > Actions
app/code/Vendor/Module/Model/DataProvider.php
<?php
namespace Vendor\Module\Model\Rule;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\SalesRule\Model\ResourceModel\Rule\Collection;
use Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory;
use Magento\SalesRule\Model\Rule;
use Magento\Framework\Registry;
use Magento\SalesRule\Model\Rule\Metadata\ValueProvider;
use Magento\Framework\Json\DecoderInterface;
class DataProvider extends \Magento\SalesRule\Model\Rule\DataProvider
{
/**
* @var $collection
*/
protected $collection;
/**
* @var array
*/
protected $loadedData;
/**
* @var \Magento\Framework\Registry
*/
protected $coreRegistry;
/**
* @var \Magento\SalesRule\Model\Rule\Metadata\ValueProvider
*/
protected $metadataValueProvider;
/**
* @var DataPersistorInterface
*/
private $dataPersistor;
/**
* @var DecoderInterface
*/
protected $jsonDecoder;
/**
* Initialize dependencies.
*
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param CollectionFactory $collectionFactory
* @param Registry $registry
* @param ValueProvider $metadataValueProvider
* @param array $meta
* @param array $data
* @param DataPersistorInterface $dataPersistor
* @param DecoderInterface $jsonDecoder
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
CollectionFactory $collectionFactory,
Registry $registry,
ValueProvider $metadataValueProvider,
array $meta = [],
array $data = [],
DataPersistorInterface $dataPersistor = null,
DecoderInterface $jsonDecoder
) {
$this->collection = $collectionFactory->create();
$this->coreRegistry = $registry;
$this->metadataValueProvider = $metadataValueProvider;
$meta = array_replace_recursive($this->getMetadataValues(), $meta);
$this->dataPersistor = $dataPersistor ?? \Magento\Framework\App\ObjectManager::getInstance()->get(
DataPersistorInterface::class
);
$this->jsonDecoder = $jsonDecoder;
parent::__construct(
$name,
$primaryFieldName,
$requestFieldName,
$collectionFactory,
$registry,
$metadataValueProvider,
$meta,
$data,
$dataPersistor
);
}
protected function getMetadataValues()
{
$rule = $this->coreRegistry->registry(\Magento\SalesRule\Model\RegistryConstants::CURRENT_SALES_RULE);
return $this->metadataValueProvider->getMetadataValues($rule);
}
public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}
$items = $this->collection->getItems();
/** @var Rule $rule */
foreach ($items as $rule) {
$rule->load($rule->getId());
$rule->setDiscountAmount($rule->getDiscountAmount() * 1);
$rule->setDiscountQty($rule->getDiscountQty() * 1);
$this->loadedData[$rule->getId()] = $rule->getData();
}
$data = $this->dataPersistor->get('sale_rule');
if (!empty($data)) {
$rule = $this->collection->getNewEmptyItem();
$rule->setData($data);
$this->loadedData[$rule->getId()] = $rule->getData();
$this->dataPersistor->clear('sale_rule');
}
$jsonData = $rule->getQtyDiscount();
if ($jsonData != '') {
$discountArray = $this->jsonDecoder->decode($jsonData);
foreach ($discountArray as $key => $discount) {
$discountData[] = ["qty_discount" => $discount['qty_discount'], "record_id" => $key];
}
$this->loadedData[$rule->getId()]['qty'] = $discountData;
}
return $this->loadedData;
}
}





