How to dump all options for any attribute in Magento 2 programmatically?

The following code does not adhere to Magento’s developer recommendations. Using object manager in such a way externally is a TERRIBLE idea.

Comment out line number 2 and then run the script. Edit line 11 and put the attribute code you want to dump

<?php
header("Location: /");exit;
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();

$eavConfig = $objectManager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product', 'manufacturer');
$options   = $attribute->getSource()->getAllOptions();
$options   = array_slice($options, 1);
usort($options, function($a, $b)
{
    return strcasecmp($a['label'], $b['label']);
});
if (count($options))
    $v = "<table>";
foreach ($options as $option) {
    $v .= "<tr><td>" . $option['value'] . '</td><td>' . $option['label'] . '</td></tr>';
}
if (count($options))
    $v .= "</table>";
echo $v;
?>
   <style>
    table tr:nth-child(odd){background:#444;color:#fff}
   </style>

Posted

in

by

Comments

Leave a Reply

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