class UpgradeData implements UpgradeDataInterface {
private $eavSetupFactory;
private $logger;
public function __construct(EavSetupFactory $eavSetupFactory, Logger $logger) {
$this->eavSetupFactory = $eavSetupFactory;
$this->logger = $logger;
}
public function getModulePath() {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$directoryReader = $objectManager->get('\Magento\Framework\Module\Dir\Reader');
$moduleDirectory = $directoryReader->getModuleDir('', 'DolanNW_ProductAttributes');
return $moduleDirectory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
$attrFile = $this->getModulePath() . '/file.csv';
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
try {
/* Map Rows and Loop Through Them */
$rows = array_map( 'str_getcsv', file( $attrFile ) );
$header = array_shift($rows);
$attributes = array();
foreach($rows as $row) {
$attributes = array_combine($header, $row);
$testArray = [
'type' => $attributes["type"],
'backend' => $attributes["backend"],
'frontend' => $attributes["frontend"],
'label' => $attributes["label"],
'input' => $attributes["input"],
'class' => $attributes["class"],
'source' => $attributes["source"],
'global' => 1,
'visible' => $attributes["visible"],
'required' => $attributes["required"],
'user_defined' => $attributes["user_defined"],
'default' => $attributes["default"],
'searchable' => $attributes["searchable"],
'filterable' => $attributes["filterable"],
'comparable' => $attributes["comparable"],
'visible_on_front' => $attributes["visible_on_front"],
'used_in_product_listing' => $attributes["used_in_product_listing"],
'unique' => $attributes["unique"],
];
$this->logger->debug( $attributes["attribute_code"] );
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
$attributes["attribute_code"],
$testArray
);
}
} catch (Exception $e) {
$this->logger->critical('Error message', ['exception' => $e]);
}
}
}
Hi, I'm new to magento 2 and am stuck at this one. I'm trying to add custom attributes by reading from a csv file. All works fine except it's not showing up under local1.mag2/admin->Stores->Product. I can certainly see the attributes in the eav_attribute table in db. Any idea what am I missing here? Thanks!