1

I am using Magento 2 with Porto theme, I recently updated theme and magento now in product information I am also seeing empty attributes that are unrelated to the current products as N/A . how can I hide them?

I saw people discussing here this in Magento 1. but it doesn't work for 2.0.

please help if you know how to do it. thanks

1

The appearance of the N/A's is the product attributes occurred because of a change in \vendor\magento\module-catalog\Block\Product\View\Attributes.php Line 90 that now calls the Phrase function: if (($value instanceof Phrase || is_string($value)) && strlen($value)) {

Since this is a core file, changing it back and extending it to your theme not recommended. Instead, I extended the Magento_Catalog module to my theme and edited the attributes.phtml to look like the code sample below.

<?php foreach ($_additional as $_data): ?>
    <?php $_dataVal = $_helper->productAttribute($_product, $_data['value'], $_data['code']); ?>
    <?php if ($_dataVal <> 'N/A'): ?>
        <?php if (is_numeric($_dataVal) == true): ?>
            <?php $_dataVal = number_format($_dataVal,2,'.',''); ?>
            <?php $_dataVal = substr($_dataVal, -3) == ".00" ? substr($_dataVal, 0, -3) : $_dataVal; ?>
        <?php endif;?>
        <tr class="<?php /* @escapeNotVerified */ echo $block->escapeHtml(__($_data['code'])) ?>">
            <th class="col label" scope="row"><?php /* @escapeNotVerified */ echo $block->escapeHtml(__($_data['label'])) ?></th>
            <td class="col data" data-th="<?php /* @escapeNotVerified */ echo $block->escapeHtml(__($_data['label'])) ?>"><?php /* @escapeNotVerified */ echo $_dataVal ?></td>
        </tr>
    <?php endif;?>
<?php endforeach; ?>
  • Would you please elaborate on what your code does. It seems it does more than just not display values with "No". It seems it also rounds numbers, no? – Mohammed Joraid Jan 22 at 2:51
  • Yes, there is a little more going on there. Basically, I first check to see if the value is not 'N/A'. If the value is 'N/A', nothing is returned. If it is not 'N/A', I check to see if it's numeric. If it is numeric and the last three characters are .00 then I strip them off. Then I return the label and the data in their html format. – Carol Feb 15 at 22:02

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.