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]);

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


More Posts

What is Visual Positioning System?

A visual positioning system (VPS) is a technology that uses visual cues from the environment to determine the position and orientation of a device or

Setup Shopify Store

To set up a Shopify store, follow these steps: Go to https://www.shopify.com/ and click on the “Start free trial” button. Enter your email address and

Let's Talk