[英]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.