Search code examples
phpcontent-management-systeme-commercemagento-1.9

Display all product, product attribute value and attribute label of specific category id in magento


I have successfully display all collection of products and product attribute value from a specific category ID. However I can't figure out how to display the product attribute label. The attribute labels are hard coded but I want it to be auto generated.

How can I display all product attribute value and their respective labels that are allowed to be seen on frontend?

So far this is what I've got

<?php
$idValue = 10;
$catagory_model = Mage::getModel('catalog/category')->load($idValue); //where $category_id is the id of the category
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($catagory_model); //category filter
$collection->addAttributeToFilter('status',1); //only enabled product
$collection->addAttributeToSelect('*'); //add product attribute to be fetched 
$collection->addStoreFilter(); 
?>
<table class="full-width-tbl">
<thead>
    <tr>
        <th>Part Number</th>
        <th>Model</th>
    </tr>
</thead>
<tbody>
<?php  foreach ($collection as $_product): ?>             
    <tr itemscope itemtype="http://schema.org/IndividualProduct">
        <td itemprop="name" style="display:none;"><?php echo $_product->getPart_number() ?></td>
        <td itemprop="sku"><?php echo $_product->getModel() ?></td>
    </tr>
<?php endforeach ?>
</tbody>
</table>

Solution

  • Try this:

    This will give you Attributelabel-Attributecode-Attributevalue

      <?php  foreach ($collection as $_product):  
    
        $product_id = $_product->getId();
        $product = Mage::getModel('catalog/product')->load($product_id);
        $attributes = $product->getAttributes();
    
    
        foreach ($attributes as $attribute) { 
                $visibility = $attribute->getIsVisibleOnFront();
                $attributeLabel = $attribute->getFrontendLabel();
                $attributeValue = $attribute->getFrontend()->getValue($product);
                $attributeCode = $attribute->getAttributeCode();
    if($visibility){           
     echo $attributeLabel . '-' . $attributeCode . '-' . $attributeValue; echo "<br />";  }     
    
        }
        endforeach
         ?>