0

How to create a custom attribute for configurable product using setup script. and the drop down list out all the bundle product and the value of drop down displayed in product detail page of a configurable product if anyone know reply me.

Installdata.php

<?php
namespace Productlinks\Bundleproduct\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
 * Eav setup factory
 * @var EavSetupFactory
 */
private $eavSetupFactory;

/**
 * Init
 * @param CategorySetupFactory $categorySetupFactory
 */
public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory)
{
    $this->eavSetupFactory = $eavSetupFactory;
}

/**
 * {@inheritdoc}
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 * @SuppressWarnings(PHPMD.NPathComplexity)
 */
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $eavSetup = $this->eavSetupFactory->create();
    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'Productlinks_Bundleproduct',
        [
            'group' => 'General',
            'type' => 'varchar',
            'label' => 'Product_link',
            'input' => 'select',
            'source' => 'Productlinks\Bundleproduct\Model\Attribute\Source\Material',
            'frontend' => 'Productlinks\Bundleproduct\Model\Attribute\Frontend\Material',
            'backend' => 'Productlinks\Bundleproduct\Model\Attribute\Backend\Material',
            'required' => false,
            'sort_order' => 50,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'is_used_in_grid' => false,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => false,
            'visible' => true,
            'is_html_allowed_on_front' => true,
            'visible_on_front' => true
        ]
    );
    $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'category_attribute', [
        'type'     => 'int',
        'label'    => 'Category Attribute',
        'input'    => 'boolean',
        'source'   => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
        'visible'  => true,
        'default'  => '0',
        'required' => false,
        'global'   => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
        'group'    => 'Display Settings',
    ]);
}
}

source for drop down

        <?php

  /**
  * Copyright © 2016 Magento. All rights reserved.
  * See COPYING.txt for license details.
  */

  namespace Productlinks\Bundleproduct\Model\Attribute\Source;

  class Material extends 
  \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
  {
  /**
 * Get all options
 * @return array
 */
 protected $_attributeFactory;

 public function __construct(
 Context $context,
 \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
 \Magento\Store\Model\StoreManagerInterface $storeManager,


 \Magento\Catalog\Model\ResourceModel\Product\Attribute
\CollectionFactory
 $attributeFactory
) 
{
  $this->_eavSetupFactory = $eavSetupFactory;
  $this->_storeManager = $storeManager;
  $this->_attributeFactory = $attributeFactory;
  parent::__construct($context);


  }

 public function getAllproducts()
{
  $attributeInfo = $this->_attributeFactory->create()- 
 >addVisibleFilter();

  foreach($attributeInfo as $attributes)
  {
      $attributeId = $attributes->getAttributeId();

   }
  }
  }

I tried this but its not working

  • Please add in question what you have already tried. You can add your code and also details of your research on this. Your question is under "LOW QUALITY POST/QUESTION" so please add more details to avoid closure of this. Good luck – Jai Aug 8 '18 at 6:14
  • For adding dropdown type attribute you can use type int and options array like - magento.stackexchange.com/questions/5190/… – Gagan Aug 8 '18 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.