Magento 2 : How to display product attributes value to custom field at Sales Rule?
-
06-01-2021 - |
Question
I am trying to display Product Custom Attribute values to Sales Rule.
I've created multiselect field using ui_component.
vendor/module/view/adminhtml/sales_rule_form.xml
<field name="brand_ids" formElement="multiselect">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">sales_rule</item>
</item>
</argument>
<settings>
<validation>
<rule name="validate-brands" xsi:type="boolean">true</rule>
</validation>
<dataType>number</dataType>
<label translate="true">Brand List</label>
<dataScope>brand_ids</dataScope>
</settings>
</field>
Field is displayed same as i need.
But now My question is i would like to display values of product attribute named brand
Brand is with Dropdown type product attribute and it contains values like Nike, Puma etc etc.
How can i achieve that ??
Any suggestions Please welcome.
Thanks in advance.
Solution
You need to create functions that will return same array as toOptionArray
.
Vednor/Module/Block/File.php
public function toOptionArray()
{
$attribute = $this->eavConfig->getAttribute('catalog_product', 'brand');
$options = $attribute->getSource()->getAllOptions();
return $options;
}
Than add below code in your ui_component file.
<field name="brands_ids" formElement="multiselect"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="source" xsi:type="string">sales_rule</item> </item> </argument> <settings> <validation> <rule name="validate-brands" xsi:type="boolean">true</rule> </validation> <dataType>varchar</dataType> <label translate="true">Brand List</label> <dataScope>brands_ids</dataScope> </settings> <formElements> <multiselect> <settings> <options class="Vendor\Module\Block\File"/> </settings> </multiselect> </formElements> </field>
Hope it will work !!
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange