3

I have seen one option 'apply_to' while adding an product attribute.

ex.

$eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'sample_attribute',
        [
            'type' => 'text',
            'backend' => '',
            'frontend' => '',
            'label' => 'Sample Atrribute',
            'input' => 'text',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => true,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false,
            **'apply_to' => ''**
        ]
    );

Can anyone explain what is the use of 'apply_to' option? Thanks in Advance.

4

Magento\Bundle\Setup\Patch\Data\ApplyAttributesUpdate

If you look at this class closely ,

You will see that apply_to has value bundle

this means this attribute will only work with bundle product type.

Here vendor/magento/module-catalog/Setup/CategorySetup.php

you will see some of attribute only applicable to simple,bundle

'apply_to' => 'simple,bundle',

3

apply_to indicates that in which type of product you want to add attribute.

For ex : you can see Magento\Catalog\Setup\CategorySetup.php class

'special_price' => [
                        'type' => 'decimal',
                        'label' => 'Special Price',
                        'input' => 'price',
                        'backend' => Price::class,
                        'required' => false,
                        'sort_order' => 3,
                        'global' => ScopedAttributeInterface::SCOPE_WEBSITE,
                        'used_in_product_listing' => true,
                        'apply_to' => 'simple,virtual',
                        'group' => 'Prices',
                        'is_used_in_grid' => true,
                        'is_visible_in_grid' => false,
                        'is_filterable_in_grid' => true,
                    ],

Here you can see that, special_price attribute apply only for simple and virtual product.

2

The 'apply_to' option is used to assign to particular product type Like 'apply_to' => 'simple,configurable', Check below code to create product attribute programmatically.

<?php
namespace Vendor\ProductAttribute\Setup;

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

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;
    /**
     * Constructor
     *
     * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'sample_attribute',
            [
                'type' => 'varchar',
                'backend' => '',
                'frontend' => '',
                'label' => 'Sample Attribute',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => 1,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => null,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => 'simple,configurable',
                'system' => 1,
                'group' => 'General'
            ]
        );
    }
}

Hope this will help you.

  • Thank you very much @Kishor Thummar – Dimple Panchal Dec 24 '19 at 10:32

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.