0

Goal

  1. Create a custom boolean attribute called "is_type" & default the value to "1"
  2. Assign custom attribute to a single attribute set
  3. Products assigned to this attribute set automatically have "is_type" set to 1 when the product is saved

Code

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'is_type',
        [
            'group' => 'General',
            'type' => 'int',
            'backend' => '',
            'frontend' => '',
            'label' => 'Is Type',
            'input' => 'boolean',
            'class' => '',
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => 0,
            'required' => 1,
            'user_defined' => 0,
            'default' => '1',
            'searchable' => 1,
            'filterable' => 0,
            'comparable' => 0,
            'visible_on_front' => 0,
            'used_in_product_listing' => 1,
            'is_used_in_grid' => 1,
            'unique' => false,
            'apply_to' => 'simple',
            'attribute_set' => 'CustomAttributeSet'
        ]
    );

I can see that the attribute is created, however, creating a new product and assigning the new product to "CustomAttributeSet" does not appear to assign the attribute to the product.

Edit:

Testing the product using the steps below, I see that the value is actually correct and assigned.

  • magerun dev:console

    • $di->get('Magento\Framework\App\State')->setAreaCode('frontend')
    • $di->create('Magento\Catalog\Model\Product')->load(2051)->getData('is_type')

I was expecting magento index:reindex && magento cache:clean to update the Visual Merchandiser products for the category that I associated with this attributed. However, for whatever reason, I had to save the category in order to refresh Visual Merchandiser Products.

0

Testing the product using the steps below, I see that the value is actually correct and assigned.

  • magerun dev:console

    • $di->get('Magento\Framework\App\State')->setAreaCode('frontend')
    • $di->create('Magento\Catalog\Model\Product')->load(2051)->getData('is_type')

I was expecting magento index:reindex && magento cache:clean to update the Visual Merchandiser products for the category that I associated with this attributed. However, for whatever reason, I had to save the category in order to refresh Visual Merchandiser Products.

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.