How to get all attributes with all available labels/options in Magento2?

I did a lot of research but could not find a solution for that. so gathered some info and created this code: (it is not the best way to do it, but it works – you can use it at least one time for quick results)
the values need to be transformed if you want to see them properly in csv

Here is how to print all available attributes in backend and along with their all available options aka labels

<?php
        use \Magento\Framework\App\Bootstrap;
        error_reporting(E_ALL);
        ini_set("display_errors",1);
        include('./app/bootstrap.php');
        $bootstrap = Bootstrap::create(BP, $_SERVER);
        $objectManager = $bootstrap->getObjectManager();
        //use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
        $collection= $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory')->create();
//$attr_groups = array();
$fta=array();
$ft=array();
        foreach ($collection as $items) {
                        $attr_groups = $items->getData();
                                                if($attr_groups['frontend_input']=="select" or $attr_groups['frontend_input']=="multiselect"){
$eav=$objectManager->get('\Magento\Eav\Model\Config')->getAttribute('catalog_product',$attr_groups['attribute_code'])->getSource()->getAllOptions();
$ft[]=$attr_groups['frontend_label'];
$ft[]=$attr_groups['attribute_code'];
//echo $attr_groups['attribute_code'].'<br>'.$attr_groups['frontend_input'].'<br>'.$attr_groups['frontend_label'].'<br>'.$attr_groups['attribute_id'].'<br>';
foreach($eav as $getlabel) {
        if(is_object($getlabel['label'])){
        $r = new ReflectionObject($getlabel['label']);
$p = $r->getProperty('text');
//echo $p;
$p->setAccessible(true);
        $ft[]= $p->getValue($getlabel['label']);
        }
        else
        $ft[]= $getlabel['label'];
}
$fta[]=$ft;
$ft=array();
}

Posted

in

,

by

Comments

Leave a Reply

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