0

I want to add new Tab in Product Edit/New Section in Admin Panel. I want to call PHTML file with dropdown. At time of saving product that dropdown value will be save in Database and while Edit it will be pre selected.

Can anyone please tell me how can i achieve this functionality in Magento 2.1.2?

Thanks in advance.

2
  • Did You try custom Attribute in attribute set instead of calling phtml
    – Magento 2
    Commented Mar 30, 2017 at 5:39
  • Nope.I didnt try it. Do you have any example for it? Commented Mar 30, 2017 at 5:41

2 Answers 2

1

Try this,

First Create Attribute whichever you need Dropdown, text etc

Then go to On the Admin panel, click Stores. In the Attributes section, select Attribute Set

then Open Your Attribute Set

enter image description here

Then create new tab then assign your attribute

0

Programmatically, you can do something like that :

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'criteres_de_selection',
        [
            'group' => 'General Information',
            'type' => 'int',
            'label' => 'Critères de sélection',
            'input' => 'select',
            'required' => false,
            'sort_order' => 100,
            'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
            'user_defined' => false,
            'is_used_in_grid' => false,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => false,
        ]
    );

In a module setup script.

/**
 * Find or create a matching attribute option
 *
 * @param string $attributeCode Attribute the option should exist in
 * @param string $label Label to find or add
 * @return int
 * @throws \Magento\Framework\Exception\LocalizedException
 */
public function createOrGetId($attributeCode, $label)
{
    if (strlen($label) < 1) {
        throw new \Magento\Framework\Exception\LocalizedException(
            __('Label for %1 must not be empty.', $attributeCode)
        );
    }

    // Does it already exist?
    $optionId = $this->getOptionId($attributeCode, $label);

    if (!$optionId) {
        // If no, add it.

        /** @var \Magento\Eav\Model\Entity\Attribute\OptionLabel $optionLabel */
        $optionLabel = $this->optionLabelFactory->create();
        $optionLabel->setStoreId(0);
        $optionLabel->setLabel($label);

        $option = $this->optionFactory->create();
        $option->setLabel($optionLabel);
        $option->setStoreLabels([$optionLabel]);
        $option->setSortOrder(0);
        $option->setIsDefault(false);

        $this->attributeOptionManagement->add(
            \Magento\Catalog\Model\Product::ENTITY,
            $this->getAttribute($attributeCode)->getAttributeId(),
            $option
        );

        // Get the inserted ID. Should be returned from the installer, but it isn't.
        $optionId = $this->getOptionId($attributeCode, $label, true);
    }

    return $optionId;
}

Following your need you can select a value by default in the attributes screen for this specific attribute.

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.