【问题标题】:Get list of all product attributes in magento获取magento中所有产品属性的列表
【发布时间】:2015-08-25 09:17:16
【问题描述】:

我做前端 magento 已经有一段时间了,但才刚刚开始构建模块。这是我知道如何做前端的事情,但我在我的模块中苦苦挣扎。我现在要实现的目标是在管理员中使用所有可用的产品属性填充多选。包括所有产品属性集的自定义产品属性。我不完全确定这需要什么表,因为我不想假设启用了平面类别数据。

我在系统配置的新选项卡中创建了我的管理区域,我创建了一个多选字段,该字段当前仅填充了三个静态选项。这很管用。任何人都可以通过指出正确的方向来帮助我......目前这是我到目前为止所拥有的(因为它的价值)。

   <?php
       class test_test_Model_Source 
       {
           public function toOptionArray()
           {
               return array(
                   array('value' => 0, 'label' =>'First item'),
                   array('value' => 1, 'label' => 'Second item'),
                   array('value' => 2, 'label' =>'third item'),

               );
           }
       }

/////////////////////////编辑////////////// /////////////////////p>

我觉得我可能在这里找到了一些东西,但它只返回每个属性的第一个字母(所以我不确定它是否甚至返回的属性)

public function toOptionArray()
{
    $attributes = Mage::getModel('catalog/product')->getAttributes();
    $attributeArray = array();
    foreach($attributes as $a){

            foreach($a->getSource()->getAllOptions(false) as $option){
                $attributeArray[$option['value']] = $option['label'];
            }

    }
    return $attributeArray; 
}

///////////////////////////////编辑/////////// //////////////////////

我不是非常接近,因为我现在知道该数组正在返回我想要的所有属性代码。但是它仍然只输出每个字母的第一个字母......有人知道为什么吗?

public function toOptionArray()
{
    $attributes = Mage::getModel('catalog/product')->getAttributes();
    $attributeArray = array();

    foreach($attributes as $a){
        foreach ($a->getEntityType()->getAttributeCodes() as $attributeName) {
            $attributeArray[$attributeName] = $attributeName;
        }
         break;         
    }
    return $attributeArray; 
}

【问题讨论】:

    标签: magento model


    【解决方案1】:

    我已经回答了我自己的问题。我找到了一种可行的方法,但我不确定为什么,所以如果有人可以评论和解释那将是有用的。所以虽然有 $attributeArray[$attributeName] = $attributeName;当您返回仅提供第一个字母的数组时,当涉及到 print_r 时起作用。但是,如果您执行以下操作,在我看来,这似乎在做同样的事情。我只能想象,在渲染时,它并不期待一个字符串,而是其他东西。无论如何,这是代码:

    public function toOptionArray()
    {
        $attributes = Mage::getModel('catalog/product')->getAttributes();
        $attributeArray = array();
    
        foreach($attributes as $a){
    
            foreach ($a->getEntityType()->getAttributeCodes() as $attributeName) {
    
                //$attributeArray[$attributeName] = $attributeName;
                $attributeArray[] = array(
                    'label' => $attributeName,
                    'value' => $attributeName
                );
            }
            break;
        }
        return $attributeArray; 
    }
    

    【讨论】:

      【解决方案2】:

      不需要像 Frank Clark 建议的那样做额外的循环。只需使用:

      public function toOptionArray() 
      {
          $attributes = Mage::getResourceModel('catalog/product_attribute_collection')->addVisibleFilter();
          $attributeArray = array();
      
          foreach($attributes as $attribute){
                  $attributeArray[] = array(
                      'label' => $attribute->getData('frontend_label'),
                      'value' => $attribute->getData('attribute_code')
                  );
          }
          return $attributeArray; 
      }
      

      【讨论】:

        【解决方案3】:

        你可以尝试其他方式获取属性,像这样

        $attributes = Mage::getSingleton('eav/config')
        ->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();
        

        一旦你有了属性,你就可以通过这种方式获得选项(从 magento 代码复制)

        $result = array();
        foreach($attributes as $attribute){
        foreach ($attribute->getProductAttribute()->getSource()->getAllOptions() as $option) {
            if($option['value']!='') {
                $result[$option['value']] = $option['label'];
            }
        }
        

        }

        【讨论】:

        • 你能贴出数组print_r的一部分吗?
        • 因此,如果我在第二个 foreach 循环 $attributeName 中创建图像::log,它会返回:DEBUG (7): allow_message DEBUG (7): allow_open_amount DEBUG (7): AW_commission_group DEBUG (7): box_depth DEBUG (7): box_height DEBUG (7): box_weight DEBUG (7): box_width DEBUG (7): brand DEBUG (7): can_be_gift_wrapped DEBUG (7): category_ids .....但是当我返回数组时,只有多选选项显示每个attribute_code的首字母
        • 如果你在返回什么打印之前执行'print_r($attributeArray)'?
        • 这个 : ( [allow_message] => allow_message [allow_open_amount] => allow_open_amount [AW_commission_group] => AW_commission_group [box_depth] => box_depth [box_height] => box_height....
        • 当我运行此代码时,我在第二个 foreach 上的非对象上调用成员函数 getSource()。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多