【问题标题】:Get all attributes of an attribute set that is not present in default attribute set获取默认属性集中不存在的属性集的所有属性
【发布时间】:2014-07-01 11:20:05
【问题描述】:

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

我尝试了以下代码:

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

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

【问题讨论】:

    标签: attributes custom-attributes magento-1.9


    【解决方案1】:

    这是我想出的解决方案

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多