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

Setup Shopify Store

Create a new Shopify Account (14 day Trail) Add Billing and Store Details Add domain name Make sure you register your domain through Godaddy/External domain

Let's Talk