代码之家  ›  专栏  ›  技术社区  ›  Hervé Guétin

检索属性代码

  •  5
  • Hervé Guétin  · 技术社区  · 16 年前

    一件让我抓狂的小事:)

    /catalog/product/view/type/options/configurable.phtml 模板文件。

    echo $_attribute->getLabel()
    

    到

    echo $_attribute->getAttributeId()
    

    eav_attribute 数据库的表。

    echo $_attribute->getAttributeCode()
    

    我得到一个空字符串,而有一个 attribute_code

    你能帮我找到我的属性代码吗?或者,更一般地说:如何获得一个属性的属性代码,我们现在从中获得id?

    谢谢!

    6 回复  |  直到 11 年前
        1
  •  16
  •   demijohn    14 年前

    在这种情况下,不需要重载Magento基代码。 如果打印\u r($\u attribute),您可以看到属性代码在array()中更深入,您可以通过以下方式直接在模板文件中使用它:

    echo $_attribute->getProductAttribute()->getAttributeCode();
    
        2
  •  4
  •   Vladimir    14 年前

    试试看

    $_attribute_code = Mage::getModel('eav/entity_attribute')->load($_attribute->getAttributeId())->getAttributeCode();
    
        3
  •  1
  •   Mukesh Chapagain    12 年前
    /**
     * get attribute collection
     */
    $attribute = $_product->getResource()->getAttribute('my_attribute');
    /**
     * get attribute type
     */
    $attribute->getAttributeType();
    /**
     * get attribute Label
     */
    $attribute->getFrontendLabel();
    /**
     * get attribute default value
     */
    $attribute->getDefaultValue();
    /**
     * check if the attribute is visible
     */
    $attribute->getIsVisible();
    /**
     * check if the attribute is required
     */
    $attribute->getIsRequired();
    /**
     * get attribute value
     */
    $attributeValue = Mage::getModel('catalog/product')->load($_product->getId())->getMyAttribute();
    

    下面是获取属性名称、代码和值的详细代码和说明: http://blog.chapagain.com.np/magento-how-to-get-attribute-name-and-value/

        4
  •  0
  •   Hervé Guétin    16 年前

    1- app/code/core/Mage/Eav/Model/Mysql4/Entity/Attribute/Collection.php

    添加此方法:

    public function setIdFilter($id)
        {
            if (empty($id)) {
                return $this;
            }
            if (!is_array($id)) {
                $id = array($id);
            }
            $this->getSelect()->where('main_table.attribute_id IN(?)', $id);
            return $this;
        }
    

    2-现在根据属性的id用法检索属性的代码:

    $attributeCode = Mage::getResourceModel('eav/entity_attribute_collection')
            ->setIdFilter(YOUR_ATTRIBUTE_ID) /* if using this into the configurable.phtml template file, you can use $_attribute->getAttributeId() for YOUR_ATTRIBUTE_ID */
            ->getFirstItem()
            ->getAttributeCode();
    echo $attributeCode;
    

    如此团结但如此有效。。。

    如果有人有更好的方法,我很乐意改变我的答案!

        5
  •  0
  •   Gavin    12 年前

    我可以用这个php获得属性代码:

    $productAttributeOptions = $_product->getTypeInstance(true)
      ->getConfigurableAttributesAsArray($_product);
    print_r($productAttributeOptions);
    

    还有,马根托真他妈让人沮丧。

        6
  •  0
  •   Nolwennig    11 年前

    我刚刚在magento安装中尝试了该代码,它返回了正确的属性代码。您可以尝试直接获取数据:

    echo $_attribute->attribute_code;
    

    var_dump 或 print_r $_attribute 变量以查看数据是否确实丢失。

    推荐文章