获取默认属性集中不存在的属性集的所有属性

问题描述 投票:0回答:1

如何获取默认属性集中不存在的属性集的属性列表?

我尝试了以下代码:

$attributeSetId = Mage::getModel('eav/entity_attribute_set')
                ->load($_product->getAttributeSetId())->getId();
$attributes = Mage::getModel('catalog/product_attribute_api')
                ->items($attributeSetId);

echo '<pre>';
print_r($attributes);
die();

但是这返回了一个包含所有属性的数组,包括默认属性集的属性,而我只需要只属于我的自定义属性集的属性。

php attributes magento-1.9 custom-attributes
1个回答
0
投票

这是我想出的解决方案

public function getSpecificAttributes($product) {
    //get ids of all the attributes in the default attribute set
    $entityTypeId = Mage::getModel('eav/entity')
            ->setType('catalog_product')
            ->getTypeId();
    $attributeSetName = 'Default';
    $defaultAttributeSetId = Mage::getModel('eav/entity_attribute_set')
            ->getCollection()
            ->setEntityTypeFilter($entityTypeId)
            ->addFieldToFilter('attribute_set_name', $attributeSetName)
            ->getFirstItem()
            ->getAttributeSetId();

    $defaultAttributes = Mage::getModel('catalog/product_attribute_api')->items($defaultAttributeSetId);
    $defaultAttributeCodes = array();
    foreach ($defaultAttributes as $attributes) {
        $defaultAttributeCodes[] = $attributes['code'];
    }

    //get ids of all the attributes in the attribute set specific to the current product
    $attributeSetId = $product->getAttributeSetId();
    $specificAttributes = Mage::getModel('catalog/product_attribute_api')->items($attributeSetId);
    $attributeCodes = array();
    foreach ($specificAttributes as $attributes) {
        $attributeCodes[] = $attributes['code'];
    }

    $currentAttributes = array_diff($attributeCodes, $defaultAttributeCodes);
    return $currentAttributes;
}
© www.soinside.com 2019 - 2024. All rights reserved.