【问题标题】:How do I get attribute set name?如何获取属性集名称?
【发布时间】:2010-01-19 05:43:39
【问题描述】:

我正在尝试在 Magento 产品视图模板中获取属性集名称。我可以通过$_product->getAttributeText('attribute')获取属性值,但是如何获取属性集名称呢?

我只想显示属于某个属性集的属性。

【问题讨论】:

    标签: magento attributes magento-1.9


    【解决方案1】:

    只要你有一个产品对象,你就可以像这样访问它的属性集:

    $attributeSetModel = Mage::getModel("eav/entity_attribute_set");
    $attributeSetModel->load($product->getAttributeSetId());
    $attributeSetName  = $attributeSetModel->getAttributeSetName();
    

    这将为您提供属性集的名称,然后您可以使用 strcmp 进行比较:

    if(0 == strcmp($attributeSetName, 'My Attribute Set')) {
        print $product->getAttributeText('attribute');
    }
    

    希望有帮助!

    【讨论】:

    • 一位匿名用户提出了编辑建议,将$attributeSet 更正为#attributeSetName。看起来很合理,所以我同意了。但是,我不懂这种语言,所以请检查它是否正确。
    • 最后一行应该是:$attributeSetName = $attributeSetModel->getAttributeSetName();,末尾没有')'
    • 函数strcmp的奇怪选择?为什么不用 === 直接比较?
    • @MatthewHaworth 老实说,答案来自大约五年前(天啊...),所以我真的不知道为什么。如果我不得不猜测,我可能更喜欢使用 strcmp,因为它仍然允许将其他数据类型隐式转换为字符串,但比== 更严格。在这种特定情况下,它们都很好。
    【解决方案2】:

    为了更性感,您可以将其缩短为:

    $attributeSetName = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName();
    

    【讨论】:

      【解决方案3】:

      试试下面的代码:

      $entityTypeId = Mage::getModel('eav/entity')
                      ->setType('catalog_product')
                      ->getTypeId();
      $attributeSetName   = 'Default';
      $attributeSetId     = Mage::getModel('eav/entity_attribute_set')
                          ->getCollection()
                          ->setEntityTypeFilter($entityTypeId)
                          ->addFieldToFilter('attribute_set_name', $attributeSetName)
                          ->getFirstItem()
                          ->getAttributeSetId();
      echo $attributeSetId;
      

      following article 中查找有关属性集的更多信息。

      谢谢

      【讨论】:

        【解决方案4】:

        Joe 的回答需要进行一些修改才能正常工作。

        首先应该是 $_product 而不是 $product,其次最后一行有一个错误的 ')'。

        下面的代码应该是正确的:

        $attributeSetModel = Mage::getModel("eav/entity_attribute_set");
        $attributeSetModel->load($_product->getAttributeSetId());
        $attributeSetName = $attributeSetModel->getAttributeSetName();
        

        【讨论】:

          【解决方案5】:

          如果用户决定稍后更改该文本,则与文本值进行比较可能会出现问题——这在 Magento 中对于属性集很容易做到。另一种选择是使用永远不会改变的底层 id。

          你可以通过在数据库中查找attribute_set_id列的值来得到这个

          select * from eav_attribute_set;
          

          这个数字也在管理员的编辑链接中,下面用粗体显示

          http://.../index.php/admin/catalog_product_set/edit/id/10/key/6fe89fe2221cf2f80b82ac2ae457909ce04c92c51716b3e474ecad672a2ae2f3/

          然后,您的代码将简单地使用产品的该属性。基于上面链接中 10 的 id,这只是

          if (10 == $_product->getAttributeSetId()) {
            //Do work
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-07-23
            • 1970-01-01
            • 2010-10-14
            • 2017-05-04
            • 2015-09-20
            • 1970-01-01
            • 1970-01-01
            • 2022-08-23
            相关资源
            最近更新 更多