You are using SalesRule
, you need to use CatalogRule
.
You can extends from \Magento\Backend\Block\Widget\Form\Generic
.
You need your conditions class with a constructor and method. This is an example of how it could be:
/**
* Constructor
*
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Data\FormFactory $formFactory
* @param \Magento\Rule\Block\Conditions $conditions
* @param \Magento\Backend\Block\Widget\Form\Renderer\Fieldset $rendererFieldset
* @param array $data
* @param \Magento\SalesRule\Model\RuleFactory|null $ruleFactory
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Data\FormFactory $formFactory,
\Magento\Rule\Block\Conditions $conditions,
\Magento\Backend\Block\Widget\Form\Renderer\Fieldset $rendererFieldset,
array $data = [],
\Magento\CatalogRule\Model\RuleFactory $ruleFactory = null
) {
$this->_rendererFieldset = $rendererFieldset;
$this->_conditions = $conditions;
$this->ruleFactory = $ruleFactory ?: ObjectManager::getInstance()
->get(\Magento\CatalogRule\Model\RuleFactory::class);
parent::__construct($context, $registry, $formFactory, $data);
}
/**
* Handles addition of conditions tab to supplied form.
*
* @param \Magento\SalesRule\Model\Rule $model
* @param string $fieldsetId
* @param string $formName
* @return \Magento\Framework\Data\Form
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function addTabToForm($model, $fieldsetId = 'conditions_fieldset', $formName = 'sales_rule_form')
{
if (!$model) {
$id = $this->getRequest()->getParam('id');
$model = $this->ruleFactory->create();
$model->load($id);
}
$conditionsFieldSetId = $model->getConditionsFieldSetId($formName);
$newChildUrl = $this->getUrl(
'catalog_rule/promo_catalog/newConditionHtml/form/' . $conditionsFieldSetId,
['form_namespace' => $formName]
);
/** @var \Magento\Framework\Data\Form $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);
return $form;
}
Magento\Rule\Model\Condition\Combine
andMagento\Rule\Model\Condition\AbstractCondition
.