1

How to get all attributes list with details that assigned to Used in product Listing-> Yes in custom.phtml file in Magento2

2 Answers 2

2

Using below code you will get all Attributes which has the property "Used" in product Listing-> Yes.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$attributes = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Config')->getAttributesUsedInListing();

foreach($attributes as $attribute){
   print_r($attribute);
}
-1
  • Crate Block file in your module and add below code. please update your module namespace & name in below code.

    namespace Mymodule\ProductCollection\Block;
    class ProductCollection extends \Magento\Framework\View\Element\Template
    {    
        protected $_productCollectionFactory;    
        public function __construct(
            \Magento\Backend\Block\Template\Context $context,        
            \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory                             $productCollectionFactory,        
            array $data = []
        )
        {    
            $this->_productCollectionFactory = $productCollectionFactory;    
            parent::__construct($context, $data);
        }
    
        public function getProductCollection()
        {
            $collection = $this->_productCollectionFactory->create();
            $collection->addAttributeToSelect('*');
            $collection->setPageSize(3); // fetching only 3 products
            return $collection;
        }
    }
    
  • Show product collection in PHTML file

$productCollection = $block->getProductCollection();

foreach ($productCollection as $product) {
    print_r($product->getData());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.