I have added custom catalog conditions to my custom module admin form. a condition drop-down is displayed fine and in post data, condition value is also getting. but I am unable to save condition value. below is my post condition array response.
[form_key] => dnqdkqI5vWvjfRnh
[conditions] => Array
(
[1] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionCombine
[aggregator] => all
[value] => 1
[new_child] =>
)
[1--1] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionProduct
[attribute] => sku
[operator] => ==
[value] => 24-WB06, 24-WB03
)
)
In admin side condition look like this.
Below is my code.
<fieldset name="storeproducts">
<settings>
<collapsible>true</collapsible>
<label translate="true">Products Of The Store</label>
</settings>
<container name="conditions_apply_to" sortOrder="10">
<htmlContent name="html_content">
<block name="conditions_apply_to" class="NamespaceModulenameBlockAdminhtmlTestEditTabConditions" />
</htmlContent>
</container>
</fieldset>
Condition block file:
<?php
namespace NamespaceModulenameBlockAdminhtmlTestEditTab;
use MagentoFrameworkAppObjectManager;
class Conditions extends MagentoBackendBlockWidgetFormGeneric
{
/**
* Core registry
*
* @var MagentoBackendBlockWidgetFormRendererFieldset
*/
protected $_rendererFieldset;
/**
* @var MagedelightProductlabelBlockConditions
*/
protected $_conditions;
/**
* @var NamespaceModulenameModelTestFactory
*/
private $TestFactory;
/**
* @var string
*/
protected $_nameInLayout = 'conditions_apply_to';
/**
* Conditions constructor.
* @param MagentoBackendBlockTemplateContext $context
* @param MagentoFrameworkRegistry $registry
* @param MagentoFrameworkDataFormFactory $formFactory
* @param MagentoRuleBlockConditions $conditions
* @param MagentoBackendBlockWidgetFormRendererFieldset $rendererFieldset
* @param NamespaceModulenameModelTestFactory|null $TestFactory
* @param array $data
*/
public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoFrameworkRegistry $registry,
MagentoFrameworkDataFormFactory $formFactory,
MagentoRuleBlockConditions $conditions,
MagentoBackendBlockWidgetFormRendererFieldset $rendererFieldset,
NamespaceModulenameModelTestFactory $TestFactory = null,
array $data = []
) {
$this->_conditions = $conditions;
$this->_rendererFieldset = $rendererFieldset;
$this->TestFactory = $TestFactory ?: ObjectManager::getInstance()
->get(MagentoCatalogRuleModelRuleFactory::class);
parent::__construct($context, $registry, $formFactory, $data);
}
/**
* {@inheritdoc}
* @codeCoverageIgnore
*/
public function getTabClass()
{
return null;
}
/**
* {@inheritdoc}
*/
public function getTabUrl()
{
return null;
}
/**
* {@inheritdoc}
*/
public function isAjaxLoaded()
{
return false;
}
/**
* {@inheritdoc}
*/
public function getTabLabel()
{
return __('Conditions');
}
/**
* {@inheritdoc}
*/
public function getTabTitle()
{
return __('Conditions');
}
/**
* {@inheritdoc}
*/
public function canShowTab()
{
return true;
}
/**
* {@inheritdoc}
*/
public function isHidden()
{
return false;
}
/**
* Prepare form before rendering HTML
*
* @return $this
*/
protected function _prepareForm()
{
$model = $this->_coreRegistry->registry(NamespaceModulenameModelRegistryConstants::CURRENT_STORE_RULE);
$form = $this->addTabToForm($model);
$this->setForm($form);
return parent::_prepareForm();
}
/**
* Handles addition of conditions tab to supplied form.
*
* @param MagentoCatalogRuleApiDataRuleInterface $model
* @param string $fieldsetId
* @param string $formName
* @return MagentoFrameworkDataForm
* @throws MagentoFrameworkExceptionLocalizedException
*/
protected function addTabToForm($model, $fieldsetId = 'conditions_fieldset', $formName = 'magedelight_Test_form')
{
if (!$model) {
$id = $this->getRequest()->getParam('id');
$model = $this->TestFactory->create();
$model->load($id);
}
$conditionsFieldSetId = $model->getConditionsFieldSetId($formName);
$newChildUrl = $this->getUrl(
'catalog_rule/promo_catalog/newConditionHtml/form/' . $conditionsFieldSetId,
['form_namespace' => $formName]
);
/** @var MagentoFrameworkDataForm $form */
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('rule_');
$renderer = $this->_rendererFieldset->setTemplate(
'Magento_CatalogRule::promo/fieldset.phtml'
)->setNewChildUrl(
$newChildUrl
)->setFieldSetId(
$conditionsFieldSetId
);
$fieldset = $form->addFieldset(
$fieldsetId,
[
'legend' => __(
'Apply the rule only if the following conditions are met (leave blank for all products).'
)
]
)->setRenderer(
$renderer
);
$fieldset->addField(
'conditions',
'text',
[
'name' => 'conditions',
'label' => __('Conditions'),
'title' => __('Conditions'),
'required' => true,
'data-form-part' => $formName
]
)->setRule(
$model
)->setRenderer(
$this->_conditions
);
$form->setValues($model->getData());
$this->setConditionFormName($model->getConditions(), $formName, $conditionsFieldSetId);
return $form;
}
/**
* Handles addition of form name to condition and its conditions.
*
* @param MagentoRuleModelConditionAbstractCondition $conditions
* @param string $formName
* @param string $jsFormName
* @return void
*/
private function setConditionFormName(MagentoRuleModelConditionAbstractCondition $conditions, $formName, $jsFormName)
{
$conditions->setFormName($formName);
$conditions->setJsFormObject($jsFormName);
if ($conditions->getConditions() && is_array($conditions->getConditions())) {
foreach ($conditions->getConditions() as $condition) {
$this->setConditionFormName($condition, $formName, $jsFormName);
}
}
}
}
Model File:
<?php
namespace NamespaceModulenameModel;
use NamespaceModulenameApiDataTestInterfaceFactory;
use NamespaceModulenameApiDataTestInterface;
//use MagentoFrameworkModelAbstractModel;
use MagentoFrameworkApiAttributeValueFactory;
use MagentoFrameworkApiExtensionAttributesFactory;
use MagentoRuleModelAbstractModel;
use MagentoFrameworkApiDataObjectHelper;
use MagentoCatalogRuleModelRuleActionCollectionFactory as RuleCollectionFactory;
class Test extends AbstractModel implements TestInterface
{
/** @var NamespaceModulenameModelTestConditionCombineFactory */
protected $condCombineFactory;
/** @var MagentoSalesRuleModelRuleConditionProductCombineFactory */
protected $condProdCombineF;
/**
* @var RuleCollectionFactory
*/
private $actionCollectionFactory;
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'productlabel_rules';
/**
* Parameter name in event
*
* In observe method you can use $observer->getEvent()->getRule() in this case
*
* @var string
*/
protected $_eventObject = 'rule';
/**
* Store already validated addresses and validation results
*
* @var array
*/
protected $validatedAddresses = [];
/**
* Test constructor.
* @param MagentoFrameworkModelContext $context
* @param MagentoFrameworkRegistry $registry
* @param MagentoFrameworkDataFormFactory $formFactory
* @param MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate
* @param TestConditionCombineFactory $condCombineFactory
* @param RuleCollectionFactory $actionCollectionFactory
* @param MagentoSalesRuleModelRuleConditionProductCombineFactory $condProdCombineF
* @param MagentoFrameworkModelResourceModelAbstractResource|null $resource
* @param MagentoFrameworkDataCollectionAbstractDb|null $resourceCollection
* @param array $data
* @param ExtensionAttributesFactory|null $extensionFactory
* @param AttributeValueFactory|null $customAttributeFactory
* @param MagentoFrameworkSerializeSerializerJson|null $serializer
*/
public function __construct(
MagentoFrameworkModelContext $context,
MagentoFrameworkRegistry $registry,
MagentoFrameworkDataFormFactory $formFactory,
MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate,
NamespaceModulenameModelTestConditionCombineFactory $condCombineFactory,
RuleCollectionFactory $actionCollectionFactory,
MagentoSalesRuleModelRuleConditionProductCombineFactory $condProdCombineF,
MagentoFrameworkModelResourceModelAbstractResource $resource = null,
MagentoFrameworkDataCollectionAbstractDb $resourceCollection = null,
array $data = [],
ExtensionAttributesFactory $extensionFactory = null,
AttributeValueFactory $customAttributeFactory = null,
MagentoFrameworkSerializeSerializerJson $serializer = null
) {
$this->condCombineFactory = $condCombineFactory;
$this->condProdCombineF = $condProdCombineF;
$this->actionCollectionFactory = $actionCollectionFactory;
parent::__construct($context, $registry, $formFactory, $localeDate, $resource, $resourceCollection, $data, $extensionFactory, $customAttributeFactory, $serializer);
}
/**
* {@inheritdoc}
*/
protected function _construct()
{
parent::_construct();
$this->_init(NamespaceModulenameModelResourceModelTest::class);
$this->setIdFieldName('Test_id');
}
/**
* Get rule condition combine model instance
*
* @return NamespaceModulenameModelTestConditionCombine
*/
public function getConditionsInstance()
{
return $this->condCombineFactory->create();
}
/**
* Get rule condition product combine model instance
*
* @return MagentoSalesRuleModelRuleConditionProductCombine
*/
public function getActionsInstance()
{
return $this->condProdCombineF->create();
}
/**
* Check cached validation result for specific address
*
* @param Address $address
* @return bool
*/
public function hasIsValidForAddress($address)
{
$addressId = $this->_getAddressId($address);
return isset($this->validatedAddresses[$addressId]) ? true : false;
}
/**
* Set validation result for specific address to results cache
*
* @param Address $address
* @param bool $validationResult
* @return $this
*/
public function setIsValidForAddress($address, $validationResult)
{
$addressId = $this->_getAddressId($address);
$this->validatedAddresses[$addressId] = $validationResult;
return $this;
}
/**
* Get cached validation result for specific address
*
* @param Address $address
* @return bool
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
*/
public function getIsValidForAddress($address)
{
$addressId = $this->_getAddressId($address);
return isset($this->validatedAddresses[$addressId]) ? $this->validatedAddresses[$addressId] : false;
}
/**
* Return id for address
*
* @param Address $address
* @return string
*/
private function _getAddressId($address)
{
if ($address instanceof Address) {
return $address->getId();
}
return $address;
}
/**
* @param string $formName
* @return string
*/
public function getConditionsFieldSetId($formName = '')
{
return $formName . 'rule_conditions_fieldset_' . $this->getId();
}
}
What is the process to save condition value to the database?
Any help would be appreciated!