2014-07-01 91 views
0

如何獲取缺省屬性集中不存在的屬性集的屬性列表?獲取缺省屬性集中不存在的屬性集的所有屬性

我嘗試了以下代碼:

$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(); 

不過這回的所有屬性,包括默認屬性集,而我只需要只屬於我的自定義屬性設置的屬性的屬性的數組。

回答

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; 
} 
解決方案