0

I want to get all product and display them in a system config multi-select field list.

I have created the system.xml and added

<field id="productlist" translate="label" type="multiselect" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Select Product</label>
    <source_model>Rahul\HelloWorld\Model\Config\Productlist</source_model>
</field>

How to create toOptionArray() in the product list(Rahul\HelloWorld\Model\Config\Productlist.php)

2

@Rahul, here is your answer, but improved to use dependency injection and also to improve the performance (i.e. Only selecting the attributes you are using rather than adding all attributes to the collection):

<?php
/**
 * Rahul
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this module to newer
 * versions in the future. If you wish to customize this module for your
 * needs please contact Rahul for more information.
 *
 * @package     Rahul\HelloWorld
 * @copyright   Copyright (c) 2018 Rahul
 * @author      Rahul
 *
 */

namespace Rahul\HelloWorld\Model\Config;

use Magento\Framework\Option\ArrayInterface;

class Productlist implements ArrayInterface
{
    /**
     * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
     */
    protected $collectionFactory;

    /**
     * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
     */
    public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
    ) {
        $this->collectionFactory = $collectionFactory;
    }

    /**
     * Retrieve all options array
     *
     * @return array
     */
    public function toOptionArray()
    {
        $collection = $this->collectionFactory->create();
        $collection
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('name');

        $ret        = [];
        foreach ($collection as $product) {
            $ret[] = [
                'value' => $product->getSku(),
                'label' => $product->getName(),
            ];
        }

        return $ret;
    }
}
1
  • yup, that will be good idea Jul 26 '18 at 13:09
0

I tried to get the product collection and passed the value in array and it worked ;)

File path: Rahul\HelloWorld\Model\Config\Productlist.php

namespace Rahul\HelloWorld\Model\Config;

use Magento\Framework\Option\ArrayInterface;

class Productlist implements ArrayInterface
{

    public function toOptionArray()
    {

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
    $collection = $productCollection->addAttributeToSelect('sku')->addAttributeToSelect('name');
    $ret = [];
    foreach ($collection as $product){

    $ret[] = [
                'value' => $product->getSku(),
                'label' => $product->getName()
            ];
    } 
    return $ret; 

    }

}

system here

Hope this will be useful for others..(any edit are most welcome)

1
  • It's bad practice to use the object manager in your own code - inject the dependencies instead. Jul 26 '18 at 12:17

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.