1

In magento 2, How to get available sales rules value in the multiselect field in admin form

Expectation:

enter image description here

  • Is this a sales rules title? – Ranganathan Nov 13 at 8:32
  • yes, its sales rule title – divya sekar Nov 13 at 9:39
1

Try below method to get available sales rules names in admin configuration

app/code/Vendor/Module/etc/adminhtml/system.xml

<field id="list_mode" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
   <label>Shopping cart rule</label>
   <source_model>Vendor\Module\Model\ConfigSource</source_model>
</field>

app/code/Vendor/Module/Model/ConfigSource.php

<?php
namespace Vendor/Module\Model;

class ConfigSource implements \Magento\Framework\Option\ArrayInterface
{
    protected $ruleFactory;
    public function __construct(
        \Magento\SalesRule\Model\RuleFactory $ruleFactory
    ){
        $this->ruleFactory = $ruleFactory;
    }

    public function toOptionArray()
    {
        $rules = $this->ruleFactory->create()->getCollection();
        $data[] = [
            'value' => '',
            'label' => __('Please select')
        ];
        foreach ($rules as $key => $rule) {
            $data[$key] = [
                'value' => $rule->getRuleId(),
                'label' => $rule->getName()
            ];
        }
        return $data;
    }
}

Hope this will help you.

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.