3

how to get all additional attributes name and value for current product

i wrote below code but its not working

    <?php
    $_helper = $this->helper('catalog/output');
    $_product = $this->getProduct()
?>
<?php if($_additional = $this->getAdditionalData()): ?>
<?php foreach ($_additional as $_data): ?>
<?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?>
<?php endforeach;?>
<?php endif; echo "ssss";?>
1
  • Currently an empty tab is displayed in product page if there is no additional attributes. <br > I want to write an if condition to completely skip showing "Additional Attributes", if it does not have any. <br > Any help? Jan 15, 2021 at 12:00

2 Answers 2

5
<?php

     (array) $excludeAttr = array();
     (array) $data = array();
        $product = $this->getProduct();
        $attributes = $product->getAttributes();
        foreach ($attributes as $attribute) {
//            if ($attribute->getIsVisibleOnFront() && $attribute->getIsUserDefined() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
            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($_additional = $data): ?>
<?php foreach ($_additional as $_data): ?>
<?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?>
<?php endforeach;?>
<?php endif; echo "ssss";?>

Please try this

4

Reference Amit Bera's answer, I write a simple code example according to my needs, hope it will help someone search the same question.

<?php
$additional = array();
$attributes = $_product->getAttributes();
foreach ($attributes as $attribute) {
    if ($attribute->getIsVisibleOnFront()) {
        $value = $attribute->getFrontend()->getValue($_product);
        if (is_string($value) && strlen($value)) {
            $additional[$attribute->getAttributeCode()] = array(
                'label' => $attribute->getStoreLabel(),
                'value' => $value
            );
        }
    }
}
foreach ($additional as $_additional) {
    echo $_additional['label']. ': '. $_additional['value']. '<br>';
}
?>

You could edit the echo $_additional['label']. ': '. $_additional['value']. '<br>'; to custom the Additional Attributes display.

If you want more control and filter to the Additional Attributes, you could study from the Amit Bera's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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