1

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!

1

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);
}
| improve this answer | |
2

So far I have found part of possible options in Magento\Catalog\Model\ResourceModel\Eav\Attribute and Magento\Catalog\Api\Data\EavAttributeInterface

const IS_WYSIWYG_ENABLED = 'is_wysiwyg_enabled';

const IS_HTML_ALLOWED_ON_FRONT = 'is_html_allowed_on_front';

const USED_FOR_SORT_BY = 'used_for_sort_by';

const IS_FILTERABLE = 'is_filterable';

const IS_FILTERABLE_IN_SEARCH = 'is_filterable_in_search';

const IS_USED_IN_GRID = 'is_used_in_grid';

const IS_VISIBLE_IN_GRID = 'is_visible_in_grid';

const IS_FILTERABLE_IN_GRID = 'is_filterable_in_grid';

const POSITION = 'position';

const APPLY_TO = 'apply_to';

const IS_SEARCHABLE = 'is_searchable';

const IS_VISIBLE_IN_ADVANCED_SEARCH = 'is_visible_in_advanced_search';

const IS_COMPARABLE = 'is_comparable';

const IS_USED_FOR_PROMO_RULES = 'is_used_for_promo_rules';

const IS_VISIBLE_ON_FRONT = 'is_visible_on_front';

const USED_IN_PRODUCT_LISTING = 'used_in_product_listing';

const IS_VISIBLE = 'is_visible';

const SCOPE_STORE_TEXT = 'store';

const SCOPE_GLOBAL_TEXT = 'global';

const SCOPE_WEBSITE_TEXT = 'website';

From this place it's more easy to dig what attribute's options provide.

| improve this answer | |
1
'attribute_model' => '', // attribute_model
'backend' => '', // backend_model
'type' => 'int', // backend_type
'table' => '', // backend_table
'frontend' => 'Magento\Catalog\Model\Product\Attribute\Frontend\Image', // frontend_model
'input' => 'text', // frontend_input
'label' => 'Test Attribute', // frontend_label
'frontend_class' => 'validate-length maximum-length-255', // frontend_class
'source' => 'some source', // source_model
'required' => 1, // is_required
'user_defined' => 1, // is_user_defined
'default' => 'my_default_value', // default_value
'unique' => 1, // is_unique
'note' => 'some_note', // note
'input_renderer' => 'Magento\Framework\Data\Form\Element\Hidden', // frontend_input_rendered
'global' => 0, // is_global
'visible' => 1, // is_visible
'searchable' => 1, // is_searchable
'filterable' => 1, // is_filterable
'comparable' => 1, // is_comparable
'visible_on_front' => 1, // is_visible_on_front
'is_html_allowed_on_front' => 0, // is_html_allowed_on_front
'used_in_product_listing' => 0, // used_in_product_listing
'used_for_sort_by' => 1, // used_for_sort_by
'apply_to' => 'bundle', // apply_to
'visible_in_advanced_search' => 1, // visible_in_advanced_search
'position' => 1, // position
'wysiwyg_enabled' => 1, // is_wysiwyg_enabled
'used_for_promo_rules' => 1, // is_used_for_promo_rules
'is_required_in_admin_store' => 1, // ?????????????????
'is_used_in_grid' => 1, // is_used_in_grid
'is_visible_in_grid' => 1, // is_visible_in_grid
'is_filterable_in_grid' => 1, // is_filterable_in_grid
'search_weight' => 0, // ???????????????????????
'additional_data' => 'not_work', // ??????????????????????

I tried to figure out all options of an attribute and how to add them programatically.

Here is a complete list of attributes. In slash comments you have names that exist in Magento DB.

I could not figure out what is the proper key name for is_required_in_admin_store, search_weight, additional_data.

Those three posted above do not update the Magento DB columns... As far as entity_type_id and attribute_code - these ones you define in eavSetup addAttribute method as first and second argument, for example:

$eavSetup->addAttribute(Magento\Catalog\Model\Product::Entity, '_my_attr_code_',
        [
           // list of options like above
        ]);

You get eavSetup from Magento\Eav\Setup\EavSetupFactory which you can inject in __construct method. You also need Magento\Framework\Setup\ModuleDataSetupInterface. So you can go like this:

protected $eavSetupFactory;
protected $setup;

public function __construct(Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,Magento\Framework\Setup\ModuleDataSetupInterface $setup) 
{
    $this->eavSetupFactory = $eavSetupFactory;
    $this->setup = $setup;
}
public function addMyAwesomeAttribute()
{
    $eavSetup = $this->eavSetupFactory->create('setup', $this->setup);
    $eavSetup->addAttribute(Magento\Catalog\Model\Product::Entity, '_my_attr_code', 
    [
        // list of options like above
    ]);
}    
| improve this answer | |

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.