I cannot add the attribute "packaging_unit" to the products on my website.
The attribute does neither apprear in the database table eav_attribute nor on the website on the product page nor in the backend.
I have already read several tutorials, change my code multiple times, but it just doesn't work.
My module.xml looks like this:
<?xml version="1.0"?>
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Test_ProductAttributes" setup_version="0.0.2">
</module>
</config>
registration.php:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Test_ProductAttributes',
__DIR__
);
InstallData.php
<?php
namespace Test\ProductAttributes\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* Eav setup factory
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Init
* @param CategorySetupFactory $categorySetupFactory
*/
public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'packaging_unit',
[
'group' => 'General Information',
'apply_to' => 'simple,configurable,virtual,bundle,downloadable,grouped',
'type' => 'varchar',
'label' => 'Packaging unit',
'input' => 'text',
'source' => 'Test\ProductAttributes\Model\Attribute\Source\PackagingUnit',
'frontend' => 'Test\ProductAttributes\Model\Attribute\Frontend\PackagingUnit',
'backend' => 'Test\ProductAttributes\Model\Attribute\Backend\PackagingUnit',
'required' => false,
'sort_order' => 50,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'is_used_in_grid' => false,
'is_visible_in_grid' => false,
'used_in_product_listing' => true,
'is_filterable_in_grid' => false,
'visible' => true,
'is_html_allowed_on_front' => true,
'visible_on_front' => true
]
);
}
}
Model/Attribute/Source/PackagingUnit.php
class PackagingUnit extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
/**
* Get all options
* @return array
*/
public function getAllOptions()
{
return $this->_options;
}
}
Model/Attribute/Backend/PackagingUnit.php
class PackagingUnit extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
{
/**
* Validate
* @param \Magento\Catalog\Model\Product $object
* @throws \Magento\Framework\Exception\LocalizedException
* @return bool
*/
public function validate($object)
{
return true;
}
}
Model/Attribute/Frontend/PackagingUnit.php:
class PackagingUnit extends \Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend
{
public function getValue(\Magento\Framework\DataObject $object)
{
$value = $object->getData($this->getAttribute()->getAttributeCode());
return "<b>$value</b>";
}
}
In the Magento root folder I run the following commands:
php bin/magento setup:upgrade
php bin/magento setup:di:compile
Both succeed without an error, but as I said it dosen't work.
What am I doing wrong?