Question

Here is the code display attribute block in PDP page,

    <?php
       echo $this->getLayout()->createBlock('catalog/product_view_attributes')
       ->setTemplate('catalog/product/view/attributes.phtml')
       ->toHtml(); 
   ?>

If i put same code in PLP page i am getting error, how can i display my attribute block in PLP page.

Was it helpful?

Solution

You can use the below code in your list.phtml file to get the product's attribute data.

<?php
    $attributes = $_product->getAttributes();
    foreach ($attributes as $attribute) {
        if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
            $value = $attribute->getFrontend()->getValue($_product);

            if (!$_product->hasData($attribute->getAttributeCode())) {
                $value = Mage::helper('catalog')->__('N/A');
            } elseif ((string)$value == '') {
                $value = Mage::helper('catalog')->__('No');
            } elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
                $value = Mage::app()->getStore()->convertPrice($value, true);
            }

            if (is_string($value) && strlen($value)) {
                $data[$attribute->getAttributeCode()] = array(
                    'label' => $attribute->getStoreLabel(),
                    'value' => $value,
                    'code'  => $attribute->getAttributeCode()
                );
            }
        }
    }
?>
<?php if ($data) { ?>
    <?php 
        $_helper = $this->helper('catalog/output');
        $_category = null;
    ?>
    <table class="data-table" id="product-attribute-specs-table">
        <col width="25%" />
        <col />
        <tbody>
            <?php foreach ($data as $_data): ?>
                    <?php if($_helper->productAttribute($_product, $_data['value'], $_data['code'])=="No" || $_helper->productAttribute($_product, $_data['value'], $_data['code']) =="Ei" | $_helper->productAttribute($_product, $_data['value'], $_data['code'])=="???"): ?>
                    <?php else: ?> 
                        <?php if(strtolower($this->htmlEscape($this->__($_data['label']))) == 'manufacturer'): ?>
                            <?php $_category = Mage::getModel('catalog/category')->loadByAttribute('name', $_helper->productAttribute($_product, $_data['value'], $_data['code'])); ?>
                        <?php endif; ?>
                        <tr>
                            <th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
                            <td class="data">
                                <?php if($_category && $_category->getUrl() && strtolower($this->htmlEscape($this->__($_data['label']))) == 'manufacturer'): ?>
                                    <a href="<?php echo $_category->getUrl(); ?>" title="<?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']); ?>">
                                <?php endif; ?>
                                <?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?>
                                <?php if($_category && $_category->getUrl() && strtolower($this->htmlEscape($this->__($_data['label']))) == 'manufacturer'): ?>
                                    </a>
                                <?php endif; ?>
                            </td>
                        </tr>
                <?php endif; ?> 
            <?php endforeach; ?>
        </tbody>
    </table>
<?php } ?>

Hope it helps!!!

OTHER TIPS

The block class Mage_Catalog_Block_Product_View_Attributes expects to get a product object out of the registry:

    function getProduct()
    {
        if (!$this->_product) {
            $this->_product = Mage::registry('product');
        }
        return $this->_product;
    }

A PLP doesn't register a product (since there are many), so the attributes block has no data to work with.

I tried setting a product in the createBlock call, but it looks like Mage_Catalog_Block_Product_View_Attributes is not written in a way that makes this possible. This is the code that does not work for me.

<?php
        echo $this->getLayout()->createBlock('catalog/product_view_attributes')
       ->setProduct($_product->load($_product->getId()))
       ->setTemplate('catalog/product/view/attributes.phtml')
       ->toHtml(); 
?>

Note that $_product here comes from the main loop in catalog/product/list.phtml. Since it is a member of a collection rather than a directly loaded product object, I had to load it explicitly. This comes with a significant performance hit.

You may be able to customize your Magento to make this work, but I don't think it is the best approach. It would be better to load the attributes that you want to see with the PLP product collection, i.e. via their "Used in Product Listing" setting. Then adapt the catalog/product/view/attributes.phtml template content for use in your PLP template, using the $_product object that is available.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top