Skip to main content
deleted 2 characters in body
Source Link
Rakesh Jesadiya
  • 42.4k
  • 19
  • 132
  • 184

Using Directly Objectmanger is not a proper way to get result into magento 2 using __consturct() way to best manner to use magento 2.

1st way,

$obj = \Magento\Framework\App\ObjectManager::getInstance();
    $attributesList = $obj->create('Magento\Catalog\Model\Entity\Attribute')->loadByCode('catalog_product', 'activity');
    
        foreach ($attributesList->getSource()->getAllOptions() as $optionInfo) {
            //echo "<pre>";print_r($optionInfo);
            if($optionInfo['value'] != '')
                echo $optionInfo['value'].' = '.$optionInfo['label']."<br>";
        }

2nd Way Using BLock

Inside Block file

    protected $attributes;
        class Getattributes{
             public function __construct(
                \Magento\Framework\View\Element\Template\Context $context,
                \Magento\Catalog\Model\Entity\AttributeFactory $attributes
                array $data = []
            ) {
                $this->attributes = $attributes;
                parent::__construct($context, $data);
            }
        
            public function getMultiselectlist($resourcename,$attributename){
                $attributesList = $this->attributes->loadByCode($resourcename, $attributename);
                return $attributesList;
            }
        }

call inside template file

echo $block->getMultiselectlist('catalog_product',$attributes='activity');

Using Directly Objectmanger is not a proper way to get result into magento 2 using __consturct() way to best manner to use magento 2.

$obj = \Magento\Framework\App\ObjectManager::getInstance();
    $attributesList = $obj->create('Magento\Catalog\Model\Entity\Attribute')->loadByCode('catalog_product', 'activity');
    
        foreach ($attributesList->getSource()->getAllOptions() as $optionInfo) {
            //echo "<pre>";print_r($optionInfo);
            if($optionInfo['value'] != '')
                echo $optionInfo['value'].' = '.$optionInfo['label']."<br>";
        }

please let me know if any issue

Inside Block file

    protected $attributes;
        class Getattributes{
             public function __construct(
                \Magento\Framework\View\Element\Template\Context $context,
                \Magento\Catalog\Model\Entity\AttributeFactory $attributes
                array $data = []
            ) {
                $this->attributes = $attributes;
                parent::__construct($context, $data);
            }
        
            public function getMultiselectlist($resourcename,$attributename){
                $attributesList = $this->attributes->loadByCode($resourcename, $attributename);
                return $attributesList;
            }
        }

call inside template file

echo $block->getMultiselectlist('catalog_product',$attributes='activity');

Using Directly Objectmanger is not a proper way to get result into magento 2 using __consturct() way to best manner to use magento 2.

$obj = \Magento\Framework\App\ObjectManager::getInstance();
    $attributesList = $obj->create('Magento\Catalog\Model\Entity\Attribute')->loadByCode('catalog_product', 'activity');
    
        foreach ($attributesList->getSource()->getAllOptions() as $optionInfo) {
            //echo "<pre>";print_r($optionInfo);
            if($optionInfo['value'] != '')
                echo $optionInfo['value'].' = '.$optionInfo['label']."<br>";
        }

please let me know if any issue

Using Directly Objectmanger is not a proper way to get result into magento 2 using __consturct() way to best manner to use magento 2.

1st way,

$obj = \Magento\Framework\App\ObjectManager::getInstance();
    $attributesList = $obj->create('Magento\Catalog\Model\Entity\Attribute')->loadByCode('catalog_product', 'activity');
    
        foreach ($attributesList->getSource()->getAllOptions() as $optionInfo) {
            //echo "<pre>";print_r($optionInfo);
            if($optionInfo['value'] != '')
                echo $optionInfo['value'].' = '.$optionInfo['label']."<br>";
        }

2nd Way Using BLock

Inside Block file

    protected $attributes;
        class Getattributes{
             public function __construct(
                \Magento\Framework\View\Element\Template\Context $context,
                \Magento\Catalog\Model\Entity\AttributeFactory $attributes
                array $data = []
            ) {
                $this->attributes = $attributes;
                parent::__construct($context, $data);
            }
        
            public function getMultiselectlist($resourcename,$attributename){
                $attributesList = $this->attributes->loadByCode($resourcename, $attributename);
                return $attributesList;
            }
        }

call inside template file

echo $block->getMultiselectlist('catalog_product',$attributes='activity');

please let me know if any issue

added 91 characters in body
Source Link
Rakesh Jesadiya
  • 42.4k
  • 19
  • 132
  • 184

Inside Block file

    protected $attributes;
        class Getattributes{
             public function __construct(
                \Magento\Framework\View\Element\Template\Context $context,
                \Magento\Catalog\Model\Entity\AttributeFactory $attributes
                array $data = []
            ) {
                $this->attributes = $attributes;
                parent::__construct($context, $data);
            }
        
            public function getMultiselectlist($resourcename,$attributename){
                $attributesList = $this->attributes->loadByCode($resourcename, $attributename);
                return $attributesList;
            }
        }

call inside template file

echo $block->getMultiselectlist('catalog_product',$attributes='activity');

Using Directly Objectmanger is not a proper way to get result into magento 2 using __consturct() way to best manner to use magento 2.

$obj = \Magento\Framework\App\ObjectManager::getInstance();
    $attributesList = $obj->create('Magento\Catalog\Model\Entity\Attribute')->loadByCode('catalog_product', 'activity');
    
        foreach ($attributesList->getSource()->getAllOptions() as $optionInfo) {
            //echo "<pre>";print_r($optionInfo);
            if($optionInfo['value'] != '')
                echo $optionInfo['value'].' = '.$optionInfo['label']."<br>";
        }

please let me know if any issue

Inside Block file

    protected $attributes;
        class Getattributes{
             public function __construct(
                \Magento\Framework\View\Element\Template\Context $context,
                \Magento\Catalog\Model\Entity\AttributeFactory $attributes
                array $data = []
            ) {
                $this->attributes = $attributes;
                parent::__construct($context, $data);
            }
        
            public function getMultiselectlist($resourcename,$attributename){
                $attributesList = $this->attributes->loadByCode($resourcename, $attributename);
                return $attributesList;
            }
        }

call inside template file

echo $block->getMultiselectlist('catalog_product',$attributes='activity');

Using Directly Objectmanger is not a proper way to get result into magento 2 using __consturct() way to best manner to use magento 2.

$attributesList = $obj->create('Magento\Catalog\Model\Entity\Attribute')->loadByCode('catalog_product', 'activity');

    foreach ($attributesList->getSource()->getAllOptions() as $optionInfo) {
        //echo "<pre>";print_r($optionInfo);
        if($optionInfo['value'] != '')
            echo $optionInfo['value'].' = '.$optionInfo['label']."<br>";
    }

please let me know if any issue

Inside Block file

    protected $attributes;
        class Getattributes{
             public function __construct(
                \Magento\Framework\View\Element\Template\Context $context,
                \Magento\Catalog\Model\Entity\AttributeFactory $attributes
                array $data = []
            ) {
                $this->attributes = $attributes;
                parent::__construct($context, $data);
            }
        
            public function getMultiselectlist($resourcename,$attributename){
                $attributesList = $this->attributes->loadByCode($resourcename, $attributename);
                return $attributesList;
            }
        }

call inside template file

echo $block->getMultiselectlist('catalog_product',$attributes='activity');

Using Directly Objectmanger is not a proper way to get result into magento 2 using __consturct() way to best manner to use magento 2.

$obj = \Magento\Framework\App\ObjectManager::getInstance();
    $attributesList = $obj->create('Magento\Catalog\Model\Entity\Attribute')->loadByCode('catalog_product', 'activity');
    
        foreach ($attributesList->getSource()->getAllOptions() as $optionInfo) {
            //echo "<pre>";print_r($optionInfo);
            if($optionInfo['value'] != '')
                echo $optionInfo['value'].' = '.$optionInfo['label']."<br>";
        }

please let me know if any issue

Source Link
Rakesh Jesadiya
  • 42.4k
  • 19
  • 132
  • 184

Inside Block file

    protected $attributes;
        class Getattributes{
             public function __construct(
                \Magento\Framework\View\Element\Template\Context $context,
                \Magento\Catalog\Model\Entity\AttributeFactory $attributes
                array $data = []
            ) {
                $this->attributes = $attributes;
                parent::__construct($context, $data);
            }
        
            public function getMultiselectlist($resourcename,$attributename){
                $attributesList = $this->attributes->loadByCode($resourcename, $attributename);
                return $attributesList;
            }
        }

call inside template file

echo $block->getMultiselectlist('catalog_product',$attributes='activity');

Using Directly Objectmanger is not a proper way to get result into magento 2 using __consturct() way to best manner to use magento 2.

$attributesList = $obj->create('Magento\Catalog\Model\Entity\Attribute')->loadByCode('catalog_product', 'activity');

    foreach ($attributesList->getSource()->getAllOptions() as $optionInfo) {
        //echo "<pre>";print_r($optionInfo);
        if($optionInfo['value'] != '')
            echo $optionInfo['value'].' = '.$optionInfo['label']."<br>";
    }

please let me know if any issue