列出magento中设置的产品属性的值

时间:2013-09-04 10:57:06

标签: magento

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

例如,有一个名为shirts-T的属性集产品,其属性为“性别”,“衬衫大小”和“颜色”。从$ _product对象开始,如何找到属性的值,例如男士,绿色,大?

我能够通过以下方式获取属性设置值:

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

我想得到所有可用的属性集值和特定属性集的代码(即shirt-T)

3 个答案:

答案 0 :(得分:0)

$_product = Mage::getModel('catalog/product')->load($productId);

现在假设您要访问此产品的制造商的价值,请考虑以下代码。

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

答案 1 :(得分:0)

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

如果是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)
    );
}

答案 2 :(得分:0)

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

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