0

I cannot add the attribute packaging_unit to the products on my website.

The attribute does neither appear in the database table eav_attribute nor on the website on the product page nor in the backend. I'm running Magento 2.1.7

I have already read several tutorials, changed my code multiple times, but it just doesn't work.

My module.xml looks like this:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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 doesn't work.

What am I doing wrong?

0

From the script I can see that you are trying to set custom option in your attribute by usbing source model. So I think you are trying to create attribute type for dropdown or multiselect. If yes then you should pass this attribute input type as select or multiselect.

So you can change input type

'input' => 'text',
  • When I change input type to select it still doesn't work. I added a write to file operation at the start of the install method. The file never gets written. I assume the method is never called. I also deleted the module's entry in the prefix_setup_module table since this might prevent the module from being installed. But is also doesn't work. What else could prevent the module from being installed? – Jens Oct 9 '17 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.