I need to show all products collection name in custom attribute dropdown in catalog products grid in admin.
I just want to show Products dropdown like colors dropdown.
Thanks in advance.
I need to show all products collection name in custom attribute dropdown in catalog products grid in admin.
I just want to show Products dropdown like colors dropdown.
Thanks in advance.
Try Below code this will give you all the product names in dropdown as per your question.
app/code/Vendor/Module/Setup/InstallData.php
<?php
namespace Vendor\Module\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'product_collection_names',
[
'group' => 'General',
'type' => 'int',
'label' => 'Product Names',
'source' => \Vendor\Module\Model\Config\Source\Options::class,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => 'simple,configurable,virtual,bundle,downloadable'
]
);
}
}
app/code/Vendor/Module/Model/Config/Source/Options.php
<?php
namespace Vendor\Module\Model\Config\Source;
class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
public function __construct
(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection
)
{
$this->_productCollection = $productCollection->create();
$this->_productCollection->addFieldToSelect('*');
}
public function getAllOptions()
{
$products = $this->_productCollection;
foreach ($products as $product)
{
$this->_options[] = ['label' => $product->getName(), 'value' => $product->getId()];
}
return $this->_options;
}
}
I would suggest you should use the repository pattern to get the required data
<?php
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class Add<Your-Attribute-Name> implements DataPatchInterface
{
/**
* ModuleDataSetupInterface
*
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* EavSetupFactory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* constructor.
*
* @param ModuleDataSetupInterface $moduleDataSetup
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavSetupFactory = $eavSetupFactory;
}
public function apply()
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->addAttribute('catalog_product', '<attribute_name>', [
'group' => 'General',
'type' => 'int',
'label' => '<attribute_label>',
'source' => \Vendor\Module\Model\Config\Source\Options::class,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => 'simple,configurable,virtual,bundle,downloadable'
]);
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [];
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
}
<?php
namespace Vendor\Module\Model\Config\Source;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Catalog\Api\ProductRepositoryInterface;
class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
/** @var ProductRepositoryInterface */
protected $productRepository;
/** @var SearchCriteriaBuilder */
protected $searchCriteriaBuilder;
public function __construct(
ProductRepositoryInterface $productRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->productRepository = $productRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}
public function getAllOptions()
{
//->addFilter($fieldName, $fieldValue, $filterType) if you want to filter product on specific condition
$searchCriteria = $this->searchCriteriaBuilder->create();
$products = $this->productRepository->getList($searchCriteria)->getItems();
foreach ($products as $product)
{
$this->_options[] = ['label' => $product->getName(), 'value' => $product->getId()];
}
return $this->_options;
}
}