0

I've created a custom menu item for the product edit page, and now I'm trying to save it.

Custom tab

The string "theducksflyatmidnight" appears nowhere in the database after saving. I've also stepped through my save-product observer with a debugger, and I can't find "theducksflyatmidnight" anywhere within the product object.

I've been studying some Vendor/Module/Setup code in Magento 2 core. Do I have to specify my custom attribute there? And then it will be saved upon saving the product?

0

My suspicion was correct; I needed to create a custom attribute using Vendor/Module/Setup/InstallData.php. I populated the select element using a source class in the attribute.

<?php

namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'product_profile',
            [
                'group' => 'My Group',
                'type' => 'varchar',
                'input' => 'select',
                'source' => 'Vendor\Module\Block\Adminhtml\Product\Edit\Tab\ProfileSelect',
                'label' => 'Profile',
                'required' => false,
                'user_defined' => true,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'used_in_product_listing' => true,
            ]
        );
        $setup->endSetup();
    }
}

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.