0

I have to get all the attributes which have type dropdown or multiselect in system configuration.

<field id="test" translate="label" type="multiselect" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Test</label>
    <source_model>Vendor\Module\Model\Config\Source\AttSource</source_model>
</field>

AttSource.php

<?php

namespace Vendor\Module\Model\Config\Source;

use \Magento\Framework\Data\OptionSourceInterface;

class AttSource implements OptionSourceInterface {

    protected $_attributeFactory;

    public function __construct(

\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory
) {

    $this->_attributeFactory = $attributeFactory;
}

public function toOptionArray() {
    $arr = [];
    $attributeInfo = $this->_attributeFactory->getCollection()->addFieldToFilter(\Magento\Eav\Model\Entity\Attribute\Set::KEY_ENTITY_TYPE_ID, 4);

    foreach ($attributeInfo as $attributes) {
        $attributeId = $attributes->getAttributeId();
        // You can get all fields of attribute here

        $arr[$attributes->getAttributeId()] = $attributes->getFrontendLabel();
    }
    return $arr;
}
}

But above code is not working.

2

Try this code

class AttSource implements \Magento\Framework\Option\ArrayInterface
{

 protected $config;
public function __construct(
    \Magento\Catalog\Model\Config $config
) 
{
    $this->config=$config;
}
public function toOptionArray()
{
    $attributes=$this->config->getAttributesUsedInProductListing();

    $attributesArray = array();
    $attributesArray = array(
        array(
        'label' => __('Please Select'),
        'value' => ''
    )

    );
    $input_type_arr = array('select', 'multiselect'); 
    foreach($attributes as  $attribute){
        if (in_array($attribute->getFrontendInput(), $input_type_arr)):
            $attributesArray[] = array('value' => $attribute->getAttributeCode(), 'label' => $attribute->getFrontendLabel());
        endif;

    }
    return $attributesArray;
}

}
0

Please try below code it's working fine for me.

<field id="allowattributesinview" translate="label comment" type="multiselect" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Simple Product Attributes to show in product detail page</label>
                    <source_model><vendor>\<module>\Model\Resource\ViewPageAttributes</source_model>
                    <comment>Selected attribute will display in configure product detail page.</comment>
                </field>

for options is below :

<?php

namespace <vendor>\<module>\Model\Resource;

class ViewPageAttributes {

    protected $_attributeFactory;

    public function __construct(
        \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory
    ){
        $this->_attributeFactory = $attributeFactory;
    }

    public function toOptionArray(){

        $attributesArray = array();
        $attributes = $this->_attributeFactory->getCollection()->addFieldToSelect('*');

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

Change code as per your need.

Still facing issue let me know.

  • How can I add filter to get attributes which are dropdown and multiselect which are used for products? – Magecode Oct 11 '18 at 6:40
  • could you please explain more about your comment – Dharmendra Jadav Oct 11 '18 at 6:43
  • I want to get attributes which are only for products or from all attribute sets only. – Magecode Oct 11 '18 at 6:46
0

Try this to get all the product attributes.

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.