cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to add product or customer attribute programmatically

Not able to add product or customer attribute programmatically

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?

2 REPLIES

Re: Not able to add product or customer attribute programmatically

I suggest you to consider installing the following Magento customer attributes extension. It's easy and you can customize the different features. It allows you to add 11 different types of fields to either registration or account page or both the pages. You can validate the input from customers, make the fields optional or mandatory, set a default value, hide them from customer after taking value etc.

 

For all features, visit: Magento Customer Attributes Extension

Re: Not able to add product or customer attribute programmatically

Its very simple to add customer attribute programetically, take a look this post will help youto add custom attribute.

https://magearray.blogspot.in/2017/11/how-to-add-custom-attribute-to-customer.html

 

Cheers

-
Magento Programmer | Was my answer helpful? You can accept it as a solution.