Magento 2 – Adding Attribute with All Possible Options

attributesmagento2setup-script

I was searching throughout net i have not found complete tutorial.
The case is I need complete configuration for newly added attribute (for product) and explantation of use of each one, adding it with use of sqlk script. If you have some notes somewhere about it, it would be extremely useful for me to can look at them.

Of course if I can find it somewhere in core, it will be ok, but so far I have not found complete sql script.

For now I have gathered:

$eavSetup->addAttribute(
            Product::ENTITY,
            'licensed_soft',
            [
                'type' => 'int',
                'input' => 'select',
                'frontend_class' => 'required-entry integer',
                'label' => 'Licensed software',

                'group' => 'License details',
                'sort_order' => 10,

                'backend' => '',
                'frontend' => '',
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',

                'default' => 0,


                'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => true,
                'user_defined' => true,

                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                //'apply_to' => ''
            ]
        );

Some of attributes I have to add are connected only with backend and some of them should be visible on front etc. Is it only visible_on_front? Or comparable or filterable mean something on front? What is product listing?

If anyone knows frontend class to validate min length of provided content of input it would be also useful.

Thanks in advance!

Best Answer

like you I was not able to find any official documentation for this, that is unfortunate. Personally I was able to get some information from random tutorial examples like this one: https://www.atwix.com/magento/adding-attribute-programatically-magento2/

However an interesting thing is that by doing a search for the string "used_in_product_listing" on the vendor/magento folder turned me to the Magento\Catalog\Model\ResourceModel\Eav\Attribute which seems to contain the list of existing options for EAV attributes and other interesting information like the list of allowed input types:

 public function isAllowedForRuleCondition()
{
    $allowedInputTypes = [
        'boolean',
        'date',
        'datetime',
        'multiselect',
        'price',
        'select',
        'text',
        'textarea',
        'weight',
    ];
    return $this->getIsVisible() && in_array($this->getFrontendInput(), $allowedInputTypes);
}

This might not contain all the info you are looking for but at least it flashes the name of a few options you can use in your attribute definitions (without any help on what they actually do, but some of the names are pretty self explanatory)

public function getIsWysiwygEnabled()
{
    return $this->getData(self::IS_WYSIWYG_ENABLED);
}

/**
 * {@inheritdoc}
 */
public function getIsHtmlAllowedOnFront()
{
    return $this->getData(self::IS_HTML_ALLOWED_ON_FRONT);
}

/**
 * {@inheritdoc}
 */
public function getUsedForSortBy()
{
    return $this->getData(self::USED_FOR_SORT_BY);
}

/**
 * {@inheritdoc}
 */
public function getIsFilterable()
{
    return $this->getData(self::IS_FILTERABLE);
}

/**
 * {@inheritdoc}
 */
public function getIsFilterableInSearch()
{
    return $this->getData(self::IS_FILTERABLE_IN_SEARCH);
}

/**
 * {@inheritdoc}
 */
public function getIsUsedInGrid()
{
    return (bool)$this->getData(self::IS_USED_IN_GRID);
}

/**
 * {@inheritdoc}
 */
public function getIsVisibleInGrid()
{
    return (bool)$this->getData(self::IS_VISIBLE_IN_GRID);
}

/**
 * {@inheritdoc}
 */
public function getIsFilterableInGrid()
{
    return (bool)$this->getData(self::IS_FILTERABLE_IN_GRID);
}

/**
 * {@inheritdoc}
 */
public function getPosition()
{
    return $this->getData(self::POSITION);
}

/**
 * {@inheritdoc}
 */
public function getIsSearchable()
{
    return $this->getData(self::IS_SEARCHABLE);
}

/**
 * {@inheritdoc}
 */
public function getIsVisibleInAdvancedSearch()
{
    return $this->getData(self::IS_VISIBLE_IN_ADVANCED_SEARCH);
}

/**
 * {@inheritdoc}
 */
public function getIsComparable()
{
    return $this->getData(self::IS_COMPARABLE);
}

/**
 * {@inheritdoc}
 */
public function getIsUsedForPromoRules()
{
    return $this->getData(self::IS_USED_FOR_PROMO_RULES);
}

/**
 * {@inheritdoc}
 */
public function getIsVisibleOnFront()
{
    return $this->getData(self::IS_VISIBLE_ON_FRONT);
}

/**
 * {@inheritdoc}
 */
public function getUsedInProductListing()
{
    return $this->getData(self::USED_IN_PRODUCT_LISTING);
}

/**
 * {@inheritdoc}
 */
public function getIsVisible()
{
    return $this->getData(self::IS_VISIBLE);
}
//@codeCoverageIgnoreEnd

/**
 * {@inheritdoc}
 */
public function getScope()
{
    if ($this->isScopeGlobal()) {
        return self::SCOPE_GLOBAL_TEXT;
    } elseif ($this->isScopeWebsite()) {
        return self::SCOPE_WEBSITE_TEXT;
    } else {
        return self::SCOPE_STORE_TEXT;
    }
}

/**
 * Set whether WYSIWYG is enabled flag
 *
 * @param bool $isWysiwygEnabled
 * @return $this
 */
public function setIsWysiwygEnabled($isWysiwygEnabled)
{
    return $this->setData(self::IS_WYSIWYG_ENABLED, $isWysiwygEnabled);
}

/**
 * Set whether the HTML tags are allowed on the frontend
 *
 * @param bool $isHtmlAllowedOnFront
 * @return $this
 */
public function setIsHtmlAllowedOnFront($isHtmlAllowedOnFront)
{
    return $this->setData(self::IS_HTML_ALLOWED_ON_FRONT, $isHtmlAllowedOnFront);
}

/**
 * Set whether it is used for sorting in product listing
 *
 * @param bool $usedForSortBy
 * @return $this
 */
public function setUsedForSortBy($usedForSortBy)
{
    return $this->setData(self::USED_FOR_SORT_BY, $usedForSortBy);
}

/**
 * Set whether it used in layered navigation
 *
 * @param bool $isFilterable
 * @return $this
 */
public function setIsFilterable($isFilterable)
{
    return $this->setData(self::IS_FILTERABLE, $isFilterable);
}

/**
 * Set whether it is used in search results layered navigation
 *
 * @param bool $isFilterableInSearch
 * @return $this
 */
public function setIsFilterableInSearch($isFilterableInSearch)
{
    return $this->setData(self::IS_FILTERABLE_IN_SEARCH, $isFilterableInSearch);
}

/**
 * Set position
 *
 * @param int $position
 * @return $this
 */
public function setPosition($position)
{
    return $this->setData(self::POSITION, $position);
}

/**
 * Set apply to value for the element
 *
 * @param string []|string
 * @return $this
 */
public function setApplyTo($applyTo)
{
    if (is_array($applyTo)) {
        $applyTo = implode(',', $applyTo);
    }
    return $this->setData(self::APPLY_TO, $applyTo);
}

/**
 * Whether the attribute can be used in Quick Search
 *
 * @param string $isSearchable
 * @return $this
 */
public function setIsSearchable($isSearchable)
{
    return $this->setData(self::IS_SEARCHABLE, $isSearchable);
}

/**
 * Set whether the attribute can be used in Advanced Search
 *
 * @param string $isVisibleInAdvancedSearch
 * @return $this
 */
public function setIsVisibleInAdvancedSearch($isVisibleInAdvancedSearch)
{
    return $this->setData(self::IS_VISIBLE_IN_ADVANCED_SEARCH, $isVisibleInAdvancedSearch);
}

/**
 * Set whether the attribute can be compared on the frontend
 *
 * @param string $isComparable
 * @return $this
 */
public function setIsComparable($isComparable)
{
    return $this->setData(self::IS_COMPARABLE, $isComparable);
}

/**
 * Set whether the attribute can be used for promo rules
 *
 * @param string $isUsedForPromoRules
 * @return $this
 */
public function setIsUsedForPromoRules($isUsedForPromoRules)
{
    return $this->setData(self::IS_USED_FOR_PROMO_RULES, $isUsedForPromoRules);
}

/**
 * Set whether the attribute is visible on the frontend
 *
 * @param string $isVisibleOnFront
 * @return $this
 */
public function setIsVisibleOnFront($isVisibleOnFront)
{
    return $this->setData(self::IS_VISIBLE_ON_FRONT, $isVisibleOnFront);
}

/**
 * Set whether the attribute can be used in product listing
 *
 * @param string $usedInProductListing
 * @return $this
 */
public function setUsedInProductListing($usedInProductListing)
{
    return $this->setData(self::USED_IN_PRODUCT_LISTING, $usedInProductListing);
}

/**
 * Set whether attribute is visible on frontend.
 *
 * @param bool $isVisible
 * @return $this
 */
public function setIsVisible($isVisible)
{
    return $this->setData(self::IS_VISIBLE, $isVisible);
}