2 Comments

Magento 2 – How to get attribute options value of eav entity?

Apply discount if 2 of the same item from a category are added
Dispaly shipping charges inside the cart page Table

How can I get the attribute options values of eav entity?
I found solution only for magento 1.x but M2 I don’t know.
M1:

$attr = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('specialty')->getData()[0];
$attributeModel = Mage::getModel('eav/entity_attribute')->load($attr['attribute_id']);
$src =  $attributeModel->getSource()->getAllOptions();

Anyone know, show me step by step, pls!Thanks!

Related Posts

  • Magento 2 – How to provide transaction? (1)
    In magento 2, How to provide transaction ? Anyone knew, please show me step by step. I found a solution for M1.x here How Provide transaction In Magento
  • Magento 2 – How to integrate WordPress into Magento 2 systen (2)
    In Magento 2, how to integrate WordPress? And create new menu for the posts. I found this solution (here and here) for magento1.x. Please show me step by step.Thanks!
  • Get all attributes and values for products (0)
    Im trying to get all attributes & values for all my products. I select all products and then i do this: foreach($product->getAttributes() as $attribute) { $attributeCode = $attribute->getAttributeCode(); $fieldVal = […]
  • Magento 2 How to add custom sort by option (1)
    I need to add an additional filter based on created_at attribute for sorting product list by latest product. I tried to figure it using below file app/design/frontend/Vendor/ThemeName/Magento_Catalog/templates/product/list/toolbar/sorter.phtml but how can add our entity id to […]
  • Magento 2 How to add custom sort by option (1)
    I need to add an additional filter based on created_at attribute for sorting product list by latest product. I tried to figure it using below file app/design/frontend/Vendor/ThemeName/Magento_Catalog/templates/product/list/toolbar/sorter.phtml but how can add our entity id to […]
  • Magento2 and custom entity options (0)
    I'm trying to add a custom attribute to order entity. I managed to add a column to sales_order, but I cannot save any value while placing the order. I tracked down the problem in: MagentoFrameworkApiDataObjectHelper::_setDataValues where my custom column is excluded beacause it is […]
  • Payment options in Magento 2 (0)
    In Magento 1.x there was an option 'Credit Card (Saved)'. I don't see that anymore in Magento 2. But during my research online, I found that credit card can stil be enabled using Authorize.Net. Could you please let me know how the RestAPI request will look like for a credit card […]
  • Magento 2 How to add custom product relations? (1)
    I am needing to add another related products attribute to associate multiple other products. I have found a solution for Magento 1: http://inchoo.net/magento/how-to-add-custom-product-relations-in-magento/ However, I wishing to implement this in Magento 2. Any guidance is appreciated.
  • Magento 2 How to add custom product relations? (1)
    I am needing to add another related products attribute to associate multiple other products. I have found a solution for Magento 1: http://inchoo.net/magento/how-to-add-custom-product-relations-in-magento/ However, I wishing to implement this in Magento 2. Any guidance is appreciated.
  • Set aliase for index using php client library in elasticsearch? (0)
    I am creating search application using elasticsearch. I want to make indexing with zero downtime. I found one solution using aliases with client library in php. But I don't know how to implement it using client library in php.
Apply discount if 2 of the same item from a category are added
Dispaly shipping charges inside the cart page Table

2 Comments on this article

  • User November 12, 2016

    you can add to the constructor of your class an instance of MagentoEavModelConfig like this:

    protected $eavConfig;
    public function __construct(
        ...
        MagentoEavModelConfig $eavConfig,
        ...
    ){
        ...
        $this->eavConfig = $eavConfig;
        ...
    }
    

    then you can use that in your class

    $attribute = $this->eavConfig->getAttribute('catalog_product', 'attribute_code_here');
    $options = $attribute->getSource()->getAllOptions();
    

    Reply

  • User November 12, 2016

    You can do it simply call below code inside your Block file.

    <?php
    namespace VendorPackageBlock;
    
    class Blockname extends MagentoFrameworkViewElementTemplate
    {
        protected $_productAttributeRepository;
    
        public function __construct(        
            MagentoFrameworkViewElementTemplateContext $context,   
            MagentoCatalogModelProductAttributeRepository $productAttributeRepository,
            array $data = [] 
        ){        
            parent::__construct($context,$data);
            $this->_productAttributeRepository = $productAttributeRepository;
        } 
    
        public function getAllBrand(){
            $manufacturerOptions = $this->_productAttributeRepository->get('manufacturer')->getOptions();       
            $values = array();
            foreach ($manufacturerOptions as $manufacturerOption) { 
               //$manufacturerOption->getValue();  // Value
                $values[] = $manufacturerOption->getLabel();  // Label
            }
            return $values;
        }  
    }
    

    Call inside your phtml file,

    <div class="manufacturer-name">
          <?php $getOptionValue = $this->getAllBrand();?>
          <?php foreach($getOptionValue as $value){ ?>
               <span><?php echo $value;?></span>
          <?php } ?>
    </div>
    

    Thanks.

    Reply

Add a comment