Magento 2 Get custom product attribute values array from product collection

Magento Collections are powerful. We can use this to get a custom product attribute value array from a product collection with a couple of lines of code.

protected $_productCollection;

/* Constructor */
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection
) {
$this->_productCollection = $productCollection;
}

/* @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $this->_productCollectionFactory->create()
->addAttributeToSelect('custom_attribute', 'catalog_product_entity_text');
$collection->addCategoriesFilter(['eq' => $category]);
$collection->load();

if ($collection) {
$allCategoryCountries = $collection->getColumnValues('custom_attribute');
}

Share this post



Leave a Reply

Your email address will not be published. Required fields are marked *