1

I want to Create Category Attribute With Drop down
Drop down options are all list of Product Attributes like http://prntscr.com/juglsn :
I tried but it didn't works

1.In setup file

$eavSetup -> addAttribute(\Magento\Catalog\Model\Category :: ENTITY, 'cat_attr1', [
    'type' => 'int',
    'label' => 'Attribute 1',
    'input' => 'select',
    'source' => 'Xx\Yy\Model\Config\Source\ProductAttributes',
    'required' => false,
    'sort_order' => 130,
    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
    'group' => 'Product Attribute Info',
    "default" => "",
    "class"    => "",
    "note"       => ""
]);

2. Incategory_form.xmlfile

<field name="cat_attr1">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Xx\Yy\Model\Config\Source\ProductAttributes</item>
        <item name="config" xsi:type="array">
            <item name="sortOrder" xsi:type="number">130</item>
            <item name="dataType" xsi:type="string">string</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="label" xsi:type="string" translate="true">Attribute 1</item>

            <item name="notice" xsi:type="string" translate="true"></item>
             <item name="additionalClasses" xsi:type="string"></item>
        </item>
    </argument>
</field>

3. inProductAttributes.phpfile

namespace Xx\Yy\Model\Config\Source;

class ProductAttributes extends 
\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{

    protected $productModel;

    public function __construct(
        \Magento\Catalog\Model\Product $productModel
    )
    {
        $this->_productModel = $productModel;
    }


    public function getAllOptions(){

     $productAttributes = $this->_productModel->getAttributes();  

     $productAttribute = [];
     foreach ($productAttributes as $attribute) {             
        $productAttribute[$attribute->getAttributeCode()]= $attribute->getFrontendLabel();
     }
      return $productAttribute;
    }

}

When I check In backend I show Loader only when I Open category Tab

7
  • Where you want to display drop down ? Provide more details with question.
    – anonymous
    Jun 13, 2018 at 12:43
  • Those options static or dynamic? Jun 13, 2018 at 12:44
  • I want to display option in category prntscr.com/jugv9a or with new tab Jun 13, 2018 at 12:50
  • @Jeeva Chezhiyan yes all product attributes Jun 13, 2018 at 12:51
  • You need to show all product attributes or especially the above attributes you mentioned? Jun 13, 2018 at 12:54

4 Answers 4

4

We have to customize convert the below functionality toOptionArray and add in Catalog Configuration.

Vendor\Config\Model\Config\Source\ProductAttributes

public function __construct(
    \Magento\Catalog\Model\Product $productModel
)
{
    $this->_productModel = $productModel;
}

public function toOptionArray()
{

    $productAttributes = $this->_productModel->getAttributes(); 
    $productAttribute = [];
    foreach ($productAttributes as $attribute) {             
        $productAttribute[] = [ 'value' => $attribute->getAttributeCode(), 'label' => $attribute->getFrontendLabel() ];
    }
    return $productAttribute;
}

Vendor/Module/view/adminhtml/ui_component/catalog_form.xml

<fieldset name="general">
    <field name="cat_prod_attr" sortOrder="40" formElement="select">
            <settings>
                <dataType>string</dataType>
                <label translate="true">Product Attribute Name</label>                    
            </settings>
            <formElements>
                <select>
                    <settings>
                        <options class="Vendor\Module\Model\Config\Source\ProductAttributes"/>
                    </settings>
                </select>
            </formElements>
    </field>
</fieldset>

enter image description here

Hope this will help you

2
  • 1
    Check I have update question Jun 14, 2018 at 7:06
  • 1
    I solved it.and used same way you did. Anyway thanks Jun 16, 2018 at 6:28
1

Your question is seems to unclear to me. But if you want to display Product attributes as drop-down.

Here i am assuming that you trying to display drop-down at admin.

You need to create a class that will return value like, toOptionArray.

Than you can use in your category's ui_component select field and than define options for that field like below.

        <formElements>
            <select>
                <settings>
                    <options class="Vendor\Module\Block\Product\Attributes"/>
                </settings>
            </select>
        </formElements>

Block must have toOptionArray Method and it will return array same as class Magento\Config\Model\Config\Source\Locale\Country.

Hope it will resolve you problem.

4
  • Check I have update question Jun 14, 2018 at 7:06
  • Setup File i understand, But why you add source for block file. Like I've created boolean type attribute for category I used Magento\Eav\Model\Entity\Attribute\Source\Boolean.
    – anonymous
    Jun 14, 2018 at 7:38
  • because I want all product attributr not boolean Jun 14, 2018 at 8:06
  • Does your block file returns same array as i mentioned above ?
    – anonymous
    Jun 14, 2018 at 8:16
0

You can try below code for customer dropdown

app/code/VendorName/ModuleName/etc/extension_attributes.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="seller_register_plan" type="string"/>
    </extension_attributes>
</config>

app/code/VendorName/ModuleName/Setup/InstallData.php

<?php


namespace [VendorName]\[ModuleName]\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{

    private $customerSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

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

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'seller_register_plan', [
            'type' => 'varchar',
            'label' => 'Seller Register Plan',
            'input' => 'select',
            'source' => '[VendorName]\[ModuleName]\Model\Customer\Attribute\Source\SellerRegisterPlan',
            'required' => false,
            'visible' => true,
            'position' => 333,
            'system' => false,
            'backend' => ''
        ]);


        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'seller_register_plan')
            ->addData(['used_in_forms' => [
                'adminhtml_customer'
            ]]);
        $attribute->save();


    }
}

This is the source Model

[VendorName][ModuleName]\Model\Customer\Attribute\Source\SellerRegisterPlan.php

<?php


namespace [VendorName]\[ModuleName]\Model\Customer\Attribute\Source;

class SellerRegisterPlan extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{

    /**
     * getAllOptions
     *
     * @return array
     */
    public function getAllOptions()
    {
        if ($this->_options === null) {
            $this->_options = [
                ['value' => '0', 'label' => __('Select an option')],
                ['value' => 'standard', 'label' => __('Standard')],
                ['value' => 'advanced', 'label' => __('Advanced')],
                ['value' => 'premium', 'label' => __('Premium')]
            ];
        }
        return $this->_options;
    }
}

Let me know if face any issue....

0

Use the code to get all the product attribute as dropdown class AttSource implements \Magento\Framework\Option\ArrayInterface {

protected $collectionFactory;

public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $collectionFactory
) {
    $this->collectionFactory = $collectionFactory;
}

public function toOptionArray(){

    $collection = $this->collectionFactory->create();
    $collection->addFieldToSelect('*');
    $attributesArray = array();
    $attributesArray = array(
        array(
        'label' => __('None'),
        'value' => ''
        )
    );

    foreach ($collection->getItems() as $attribute) {
        $attributesArray[] = array('value'=> $attribute->getAttributeCode(),'label'=> $attribute->getFrontendLabel());
    }
    return $attributesArray;
}

}

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.