1

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.

2

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!!!

  • everything okay, but N/A and empty attribut also display – zus Sep 23 at 8:01
  • Ok, just check the data coming with the condition as you did in PDP – Sumit Sep 23 at 8:02
  • sure, thank you – zus Sep 23 at 8:04
  • In my PDP page more number of attributes has values, in my PLP attributes values shown as N/A but attribute has values. – zus Sep 23 at 8:46
  • Configurable products attributes details showing wrong, simple products attributes working good. snipboard.io/GNat2L.jpg @Sumit – zus Oct 17 at 5:33
2

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.

  • I am getting error like : Parse error: syntax error, unexpected ';', expecting ',' or ')' in C:\wamp64\www\magepro\app\design\frontend\default\theme327k\template\catalog\product\list.phtml on line 101 sourcecode : paste.ofcode.org/kpv9VzmTu8ruU5KBD7nyHh – zus Sep 23 at 4:57
  • Apologies -- there was a typo in my sample code. It is updated to add a missing ")" after "->setProduct($_product->load($_product->getId())" – jiheison Sep 23 at 14:25
  • I also see that your template is calling "<?php $_product = Mage::getModel('catalog/product')->load($_product->getId()); ?>". This will slow down the page, because it is loading each product separately from the database, rather than using the product collection that is built in to the template. It should be possible to work around this. However, if not, I would suggest moving this line higher in the loop. This will allow you to skip loading the product again when calling setProduct in the attributes block. – jiheison Sep 23 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.