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');
}
Recent Comments