2

Since I am just adding a product attribute which is stored in EAV tables, this is how InstallData.php looks like

<?php

namespace Namespace\Modulename\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
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 EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'customer_product_points',
            [
                'type' => 'decimal',
                'backend' => '',
                'frontend' => '',
                'label' => 'Customer Product Points',
                'input' => '',
                'class' => '',
                'source' => '',
                'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => 0,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => false,
                'unique' => false,
                'apply_to' => ''
            ]
        );
    }
}

Executing php magento setup:upgrade I was able to see new attribute in the system.

But the attribute is not visible while adding a new product. Have I missed anything here ? I need to add points to products and same will be reflected on frontend.

2 Answers 2

2

You need add attribute in default attribute set. You can use \Magento\Eav\Setup\EavSetup::addAttributeToGroup for this

       $categorySetup->addAttributeToGroup(
            $entityTypeId,
            $attributeSetId,
            $groups[$attributeProp['group']]['id'],
            $attributeCode,
            $attributeProp['sort']
        );
3
  • Also what about the variables like $entityTypeId, $attributeSetId,$attributeProp['sort'] ? Where do I specify them ? Commented Feb 24, 2016 at 6:14
  • Please read documentation to addAttributeToGroup method
    – KAndy
    Commented Feb 24, 2016 at 11:54
  • 1
    You can add it to general by adding 'group' => 'General', in the addAttribute method call. Magento will do the grunt work for you.
    – Michiel
    Commented Aug 13, 2016 at 14:36
0

Here is how you would add it to the default attributset, with a group name you can define (or you use an existing group name, like Product Details):

$attributeGroup = 'My Group Name'; // the group doesn't need to exist

$attributeCode = 'customer_product_points'; // or whatever attribute you want to add

$entityTypeId = $eavSetup->getEntityTypeId(
    \Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE
);

$attributeSetId = $eavSetup->getDefaultAttributeSetId($entityTypeId);

$eavSetup->addAttributeToSet($entityTypeId, $attributeSetId, $attributeGroup, $attributeCode);

Another option would be to add 'groups' => 'my group name' to your attribute array, it would then automatically be added to all attributsets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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