【问题标题】:list the values of a product attribute set in magento列出在 magento 中设置的产品属性的值
【发布时间】:2013-09-04 10:57:06
【问题描述】:

如何找到产品属性集的值?

例如,有一个产品的属性集称为衬衫 - T,其属性包括性别、衬衫尺码和颜色。从 $_product 对象开始,我如何找到属性的值,例如男士、绿色、大号?

我可以通过以下方式获取属性集值:

$product = Mage::getModel('catalog/product')->load($productId);
        $prodAttributeSet = Mage::getModel('eav/entity_attribute_set')->load($product->getAttributeSetId())->getAttributeSetName();

我想获取特定属性集的所有可用属性集值和代码(即衬衫 - T)

【问题讨论】:

    标签: magento


    【解决方案1】:
    $_product = Mage::getModel('catalog/product')->load($productId);
    

    现在假设您要访问该产品制造商的值,然后考虑以下代码。

    $manufacturerValue = $_product->getAttributeText('manufacturer');
    

    【讨论】:

    • 我想显示:颜色、尺寸...产品的所有可用属性集值
    • $color=$_product->getAttributeText('color'); $color=$_product->getAttributeText('size');
    • 我不知道一个产品有哪些可用属性,即假设一个产品存在颜色属性而不是其他产品
    【解决方案2】:

    正如您在评论中提到的尺寸和颜色,我可以给您一个示例代码供您使用。

    如果是select or multiselect 属性,您仍然需要将选项ID 映射到选项值。如果您获得的是整数列表而不是人类可读的标签(例如颜色或制造商属性),就会出现这种情况。

    // specify the select or multiselect attribute code
    $attributeCode = 'color';
    
    // build and filter the product collection
    $products = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToFilter($attributeCode, array('notnull' => true))
            ->addAttributeToFilter($attributeCode, array('neq' => ''))
            ->addAttributeToSelect($attributeCode);
    
    $usedAttributeValues = array_unique($products->getColumnValues($attributeCode));
    
    // ---- this is the different part compared to the previous example ----
    
    // fetch the attribute model
    $attributeModel = Mage::getSingleton('eav/config')
            ->getAttribute('catalog_product', $attributeCode);
    
    // map the option id's to option labels
    $usedAttributeValues = $attributeModel->getSource()->getOptionText(
        implode(',', $usedAttributeValues)
    );
    

    直接数据库查询示例

    根据您要执行此操作的位置,这里是一个不使用产品集合获取值的示例。它的效率略高。 仅在资源模型中使用以下代码,因为那是 DB 相关代码所属的地方。 这是一个教育示例,展示如何使用 Magento 的 EAV 表。

    // specify the attribute code
    $attributeCode = 'color';
    
    // get attribute model by attribute code, e.g. 'color'
    $attributeModel = Mage::getSingleton('eav/config')
            ->getAttribute('catalog_product', $attributeCode);
    
    // build select to fetch used attribute value id's
    $select = Mage::getSingleton('core/resource')
            ->getConnection('default_read')->select()
            ->from($attributeModel->getBackend()->getTable(), 'value')
            ->where('attribute_id=?', $attributeModel->getId())
            ->distinct();
    
    // read used values from the db
    $usedAttributeValues = Mage::getSingleton('core/resource')
            ->getConnection('default_read')
            ->fetchCol($select);
    
    // map used id's to the value labels using the source model
    if ($attributeModel->usesSource())
    {
        $usedAttributeValues = $attributeModel->getSource()->getOptionText(
            implode(',', $usedAttributeValues)
        );
    }
    

    【讨论】:

      【解决方案3】:

      如果您想要一个属性集的所有属性,您可以执行以下操作。

      $product = Mage::getModel('catalog/product')->load($productId);
      $attributes = $eavConfig->getEntityAttributeCodes( Mage_Catalog_Model_Product::ENTITY, $product );
      print_r(attributes);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-06
        • 1970-01-01
        • 1970-01-01
        • 2011-06-25
        • 1970-01-01
        • 2017-03-03
        相关资源
        最近更新 更多