简体   繁体   English

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

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

Here is my function for calling product attribute collection I have already get product attributes for the product's which are enabled but I am having issue in filtering them according to their own visibility ie I want only those product attributes collection whose status is set visible from 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;
 }                                                                    } 

No tested but it will do job for you 没有经过测试,但它会为你做好准备

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

To get All product attributes you need to use inject class要获取您需要使用注入类的所有产品属性

app/code/Mendor/Mymodule/Model/Attributes.php 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;
}

You can do it using the following function:您可以使用以下功能来做到这一点:

 public function getallattributes()
    {
        $arr = [];
        $attributeInfo = $this->_attributeFactory->getCollection()- 
        >addFieldToFilter(\Magento\Eav\Model\Entity\Attribute\Set::
        KEY_ENTITY_TYPE_I D, 4);
    
        foreach($attributeInfo as $attributes)
        {
            $attributeId = $attributes->getAttributeId();
            // You can get all fields of attribute here
            if($attributes->getIsVisibleOnFront()){
                $arr[$attributes->getAttributeId()] = $attributes 
                     >getFrontendLabel();
            }
        }
        return $arr;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Magento在产品详细信息页面上的自定义模块块中获取产品ID - Magento get product ID in custom module block on product detail page 如何在Magento自定义模块中为产品集合设置自定义URL? - How to set my custom URL for product collection in Magento custom module? 重用Magento产品列表块-自定义集合 - Reuse Magento product list block - custom collection 如何在magento中按库存状态对产品集合进行排序 - How to sort product collection on stock status in magento 如何在Magento中将产品属性选项添加到自定义模块中 - How to get product attribute options into a custom module in Magento 如何在magento网格视图的自定义模块中获取产品名称? - How to get Product name in custom module in magento grid view? 如何在Magento的自定义产品网格中显示是/否属性? - How to display yes/no attribute in custom product grid in Magento? magento自定义模块中带有请求参数的产品集合工具栏 - product collection toolbar with request params in magento custom module Magento-如何使用模块在产品详细信息页面中添加新的自定义模块 - Magento- How can i add a new custom block in product details page using module 如何通过表达多个属性来过滤magento产品集合 - How can I filter a magento product collection by expression of multiple attributes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM