草庐IT

php - Magento 2 : How to get product attributes collection in custom module block file whose status is visible =>yes?

coder 2024-05-02 原文

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

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;
 }                                                                    } 

最佳答案

没有测试,但它会为你工作

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

关于php - Magento 2 : How to get product attributes collection in custom module block file whose status is visible =>yes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49005649/

有关php - Magento 2 : How to get product attributes collection in custom module block file whose status is visible =>yes?的更多相关文章

随机推荐