Nov 9, 2012

Building Attribute Group Tree


Each product has a defined attribute set in Magento and which is set on creation of the product. Attribute sets consists of a number of attribute groups which in turn has a number of attributes.

Normally that is used only in admin, but I got a request to classify the attributes like that in a grid on frontend, which was a bit tricky. Below is the solution that I came up to.


$retValue = array();

/* @var $product Mage_Catalog_Model_Product  */
$product = Mage::getModel('catalog/product')->load(3224);

// Get the attribute set of the product
$attributeSetId = $product->getAttributeSetId();

// Get the attribute groups associated to the attribute set
$attributeGroups = Mage::getResourceModel('eav/entity_attribute_group_collection')
    ->setAttributeSetFilter($attributeSetId)
    ->load();


// Get the attributes of each group
foreach ($attributeGroups as $value) {
    $group['title'] = $value->getAttributeGroupName();

    $attributes = $product->getAttributes($value->getId());

    $attributeVal = array();

    foreach ($attributes as $attribute) {
        $att['title'] = $attribute->getFrontEndLabel();
        $attributeCode = $attribute->getAttributeCode();

        
        $data = $product->getData($attributeCode);
        if (!empty($data['values'])) {
            $attributeVal = $product->getAttributeText($attributeCode);
        }

        if (!empty($attributeVal)) {
            $att['value'] = $attributeVal;
            $group['value'][] = $att;
        }
    }

        $retValue[] = $group;
}

echo '<pre>';
var_dump($retValue);
echo '</pre>';

1 comment:

  1. Provide your e mail and verification paperwork to register with a protected online on line casino. 1xbet The recreation also permits for a wide range|a variety} of bets, so that you can|so as to|to have the ability to} discover the best odds for you. You can opt for an even-money wager on red, black, odd, or even. You might go lengthy and wager on a single number, or wager on a "road" on three different numbers, for instance. You must wager on which deck will contain a matching card, wagering on both Andar or Bahar. The vendor then attracts alternately from the 2 decks till a matching card comes up.

    ReplyDelete