Magento 2:如何在状态可见的自定义模块块文件中获取产品属性集合=>是吗?

时间:2018-02-27 09:48:08

标签: php magento magento2.2

这是我调用产品属性集合的函数我已经获得了已启用的产品的产品属性但我在根据自己的可见性过滤它们时遇到问题,即我只想要那些产品属性集合状态从admin ....

设置为可见
class ProductList extends \Magento\Framework\View\Element\Template
{
protected $_attributeFactory;

public function __construct(
         \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory

         ){
    parent::__construct($context);
    $this->_attributeFactory = $attributeFactory;
    }

public function getallattributes()
{
    $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;
 }                                                                    } 

2 个答案:

答案 0 :(得分:3)

没有经过测试,但它会为你做好工作

$attributeInfo = $this->_attributeFactory->getCollection()
                 ->addFieldToFilter(\Magento\Eav\Model\Entity\Attribute\Set::KEY_ENTITY_TYPE_ID, 4)
                 ->addFieldToFilter('is_visible_on_front',1);

答案 1 :(得分:1)

要获取所有产品属性,您需要使用注入类

app / code / Mendor / Mymodule / Model / Attributes.php

 public function __construct(Context $context,   
    \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $coll
    ){
 $this->_coll=$coll;
        parent::__construct($context);
}

 public function getAllAttributes()
    {
        $this->_coll->addFieldToFilter(\Magento\Eav\Model\Entity\Attribute\Set::KEY_ENTITY_TYPE_ID, 4);
        $attrAll = $this->_coll->load()->getItems();
 echo '<pre>'; var_dump($attrAll);'</pre>';
        exit;
}