In this competitive market of E-commerce, it’s necessary to deliver something beyond the expectations to your customers. Because having a great user experience encourages your customer to purchase more and more from your store. At MageComp, we do the same we imagine, we build, try and test extension, again and again until we get satisfied to deliver a perfect product to our valuable customers. So, they do not need to worry once they set-n-up.
Recently, one of the customers came with a requirement of highlighting some of the store products by making easy backend selection to serve personalized product experience. And we started coding, at that time we have decided to display product list on system configuration to make an easy selection of products using the multi-select box. Here is another blog series to get Product List in System Configuration in Magento 2.
First we need to add this below code to ‘System.xml’ file available ad below location to add multi select box to system configuration.
app\code\vendor\extension\etc\adminhtml\system.xml
1 2 |
<label>Select Product</label> VENDOR\EXTENSION\Model\Config\Productlist |
Now create another file ‘Productlist.php’ inside your extension folder with below code to fetch and display products list.
app\code\vendor\extension\Model\Config\Productlist.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<!--?php namespace VENDOR\EXTENSION\Model\Config; use Magento\Framework\Option\ArrayInterface; class Productlist implements ArrayInterface { protected $collectionFactory; public function __construct( \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory ) { $this->collectionFactory = $collectionFactory; } 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; } } </pre> <p>Moreover, you can play with this code according to your need of personalizing product experience.<br ?--> Lastly, Comment below if you face an issue while using this code. Happy Coding! |