【问题标题】:Magento 2 : How to get product attributes collection in custom module block file whose status is visible =>yes?Magento 2:如何在状态可见=>是的自定义模块块文件中获取产品属性集合?
【发布时间】:2018-08-06 22:06:58
【问题描述】:

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

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

【问题讨论】:

    标签: php magento magento2.2


    【解决方案1】:

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

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

    【讨论】:

    • 如何过滤仅适用于产品或所有属性集的属性?
    • 没用。我必须从与产品相关的所有属性集中获取所有属性。
    • @Magecode 这正是您所要求的。否则我不清楚你在问什么
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      您可以使用以下函数来完成:

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

      【讨论】:

        猜你喜欢
        • 2017-10-13
        • 2013-04-10
        • 1970-01-01
        • 2016-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多