1

I have added following field in my UI Component Form in a custom module to get the Product Attributes ** in conditions, but the options given in the drop-down are **Cart Attributes whereas I need Product Attributes!

Here is my code,

<fieldset name="conditions">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Products with Conditions</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="number">127</item>
        </item>
    </argument>
    <container name="conditions_apply_to" >
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">10</item>
            </item>
        </argument>
        <htmlContent name="html_content">
            <argument name="block" xsi:type="object">Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Conditions</argument>
        </htmlContent>
    </container>
</fieldset>

How can I change this?

  • do you want to display your product custom attribute to product attribute select list? – Nikunj Vadariya Jun 7 '18 at 6:13
  • Please review this 2 file: Magento\Rule\Model\Condition\Combine and Magento\Rule\Model\Condition\AbstractCondition. – Dhaduk Mitesh Jun 7 '18 at 6:22
  • @NikunjVadariya I only need the default Product attributes provided by magento , no customization. – Verdu Jun 7 '18 at 6:27
  • Which default attribute you want to add inside product attribute condition? – Nikunj Vadariya Jun 7 '18 at 6:41
  • @NikunjVadariya go to content->widgets and in widgets options you can see CONDITIONS at the bottom . I need same product attributes – Verdu Jun 7 '18 at 6:45
2

You need replace follow lines in file app/code/Vendor/Rules/Model/Rule.php

Find this:

\Magento\SalesRule\Model\Rule\Condition\CombineFactory $condCombineFactory,
\Magento\SalesRule\Model\Rule\Condition\Product\CombineFactory $condProdCombineF,

Replace with:

\Magento\CatalogWidget\Model\Rule\Condition\CombineFactory $condCombineFactory,
\Magento\CatalogWidget\Model\Rule\Condition\ProductFactory $condProdCombineF,
  • what is the path for /Rules/Model/Rule.php in Magento Core Files? – Verdu Jun 7 '18 at 10:58
  • You override rule.php or not. – Dhaduk Mitesh Jun 7 '18 at 11:42
  • No, not at the moment. – Verdu Jun 7 '18 at 11:49
  • You want to change in core file first and check it. If working then override it. – Dhaduk Mitesh Jun 7 '18 at 11:54
  • Yes! so where is the actual file in core to this one app/code/Vendor/Rules/Model/Rule.php ? – Verdu Jun 8 '18 at 5:57
0

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;
}

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.