Magento: How to show unset attributes as empty not "No" / "N/A"? - magento

Magento has done great job on showing empty attributes as "No" or "N/A", but
I need them to show up as empty table cells.
I know this code hides empty attributes completely:
<?php foreach ($_additional as $_data): ?>
<?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
Source: http://www.magthemes.com/magento-blog/empty-attributes-showing-na-fix/
but as I am new to php, I don't really know how to modify it to show them empty.
I know I could go to core files and modify Attributes.php, but thats a bad practice and I wanted to do it right.
Thanks in advance!

You can write a extension thar rewrites Mage_Catalog_Block_Product_View_Attributes::getAdditionalData. But this requires much more knowledge than is necessary to modify the template.
Modify template. Your code is already done. Only few modifications need
<?php foreach ($_additional as $_data): ?>
<?php
$_attribute = $_product->getResource()->getAttribute($_data['code']);
$_isEmpty = (!is_null($_product->getData($_attribute->getAttributeCode())) &&((string)$_attribute->getFrontend()->getValue($_product) != ''));
?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php if (!$_isEmpty) { echo $_helper->productAttribute($_product, $_data['value'], $_data['code'])} ?> </td>
</tr>
<?php endforeach; ?>
P.S. Code can contain errors, have not tested :)

ALT+0160
Use the above keyboard shortcut as the attribute value when you edit your product.
How?
On Windows, at least, do the following:
Hold ALT key and type the numbers 0160 on your keypad. Now release the ALT key.
Basically a whitespace character appears that is not a space character. This will register as a non-null value for your product field and the engine will render the character which, fortunately for us humans, is a whitespace that we won't see.
It's not elegant, but it will help those who aren't able to modify template code.

i try with this options to remove "No or N/A" and ok remove this words but don´t remove atribute name. i use this code:
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct()
?>
<?php if($_additional = $this->getAdditionalData()): ?>
<div class="akordeon-item-head">
<div class="akordeon-item-head-container">
<div class="akordeon-heading">
<?php echo $this->__('Additional Information') ?>
</div>
</div>
</div>
<div class="akordeon-item-body">
<div class="akordeon-item-content">
<table class="data-table" id="product-attribute-specs-table">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional as $_data): ?>
<?php
$_attribute = $_product->getResource()->getAttribute($_data['code']);
$_isEmpty = (!is_null($_product->getData($_attribute->getAttributeCode())) &&((string)$_attribute->getFrontend()->getValue($_product) != ''));?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php if ($_isEmpty) { echo $_helper->productAttribute($_product, $_data['value'], $_data['code']);} ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
</div>
</div>
<?php endif;?>

i find this anwerd and work for me
<?php foreach ($_additional as $_data): ?>
<?php if ($_data['value'] == 'No') {continue;} ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>

Related

DataTables warning: table id=datatable - Requested unknown parameter '1' for row 0

can someone tell me what's wrong with my code? been stuck here for hours... it always says that "DataTables warning: table id=datatable - Requested unknown parameter '1' for row 0."
here's my code:
<table id="datatable" class="table table-bordered table-striped dataTable">
<thead>
<tr>
<th>Name</th>
<th></th>
</tr>
</thead>
<tbody>
<?php echo $this->session->flashdata('message'); ?>
<?php $offset = $this->uri->segment(3, 0) + 1; ?>
<?php foreach ($query->result() as $row): ?>
<tr>
<td><?php echo $row->manufacturer_name; ?></td>
<?php echo "<form method='post' action='asset_management/updateForm'>";?>
<input type="hidden" name="manufacturer_id" value="<?php echo $row->manufacturer_id;?>"/>
<?php echo form_open('asset_management/updateForm');
echo form_submit('p_submit', 'Update', "class='btn btn-warning btn-xs'"); ?>
<?php echo form_close(); ?>
</form>
<a class="btn btn-danger btn-xs" onclick="javascript:deleteConfirm('<?php echo site_url('asset_management/deletemanu/' . $row->manufacturer_id); ?>');" deleteConfirm href="#">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a></th>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Thank you so much!
no. of table heading may not matches with no. of td in the table.
It shows the mentioned error.
visit https://www.datatables.net/

How to always display the Product attribute label in English in the Additional information tabs in Magento

I want to add some styling to the product additional information table.
To do so, i have added the product attribute label to the standard table layout file located in:
app/design/frontend/my_theme/template/catalog/product/view/attributes.phtml
<?php foreach ($_additional as $_data): ?>
<?php $lbl = $this->escapeHtml($this->__($_data['label']))?>
<tr class="<?php echo strtolower(str_replace(' ', '_', $lbl)) ?>">
<th class="label"><?php echo $this->escapeHtml($_data['label']) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
the problem with this is that te label gets translated to the store language. Is there a way to force the label to always display in English?
Much appreciated!
I think you're better off using the attribute code instead of the attribute label if you just need an identifier. The attribute code is universal across all storeviews.
<?php foreach ($_additional as $_data): ?>
<?php $code = $this->escapeHtml($this->__($_data['code']))?>
<tr class="<?php echo $code; ?>">
<th class="label"><?php echo $this->escapeHtml($_data['label']) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
You should remove $this->__() from string
<?php foreach ($_additional as $_data): ?>
<?php $lbl = $this->escapeHtml($this->__($_data['label']))?>
<tr class="<?php echo strtolower(str_replace(' ', '_', $lbl)) ?>">
<th class="label"><?php echo $this->escapeHtml($_data['label']) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>

add content other than attribute information under “Additional information” in magento

we can see "Additional information " tab in magento product view page.
there we can list all attributes.
I want to display some contents under "Additional information " tab....
means
Main information
attribute label 1 : attribute value
Sub - information
attribute label 2 : atribute value.
please let me know if you need any clarifications.
thanks in advance.
As answered on Magento SE:
You could customize the template
catalog/product/view/attributes.phtml. Copy it to your theme from
base/default if it isn't there yet and include what you need.
For better maintainability, I'd recommend to just add
$this->getChildHtml('my_child_alias') to the template and define
child blocks in the layout (i.e. layout/local.xml of your theme:
<reference name="product.attributes">
<block type="core/template" name="my.custom.product.block" as="my_child_alias" template="my/custom/template.phtml" />
</reference>
Judging by the additional information from your comments here, you probably don't want to create child blocks, instead you'll have to add code within the foreach loop.
Original
<?php foreach ($_additional as $_data): ?>
<tr>
<th class="label"><?php echo $this->escapeHtml($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
Changed (example)
<?php foreach ($_additional as $_data): ?>
<?php if ($_data['code'] == 'attribute_1'): ?>
<tr><th colspan="2"><?php echo $this->__('Main Information') ?></th></tr>
<?php elseif ($_data['code'] == 'attribute_2'): ?>
<tr><th colspan="2"><?php echo $this->__('Sub Information') ?></th></tr>
<?php endif; ?>
<tr>
<th class="label"><?php echo $this->escapeHtml($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
where attribute_1 and attribute_2 are the codes of the first attributes of each group.
Modify File : catalog\product\view\attributes.phtml.
Replace <th></th> by this code.
<th class="label">
<?php if(array_key_exists('size',$_additional) && array_key_exists('color',$_additional) ){
if($_data['code'] == 'size'){
echo " Main features : ";
}
} ?>
<?php echo $this->escapeHtml($this->__($_data['label'])) ?>
</th>
Apply this code and check out put.

show text field attribute of a configurable product magento

I have added configurable product and added textarea attribute to it, I want to show the attributes at front end in my custon view, how can I show ?
you can use this code
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct()
?>
<?php if($_additional = $this->getAdditionalData()): ?>
<h2><?php echo $this->__('Additional Information') ?></h2>
<table class="data-table" id="product-attribute-specs-table">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional as $_data): ?>
<tr>
<th class="label">
<?php echo $this->escapeHtml($this->__($_data['label'])) ?></th>
<td class="data">
<?php echo $_helper->productAttribute($_product, $_data['value'],
$_data['code']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
<?php endif;?>
Catalog -> Attribute -> Manage Attribute -> Select your attribute -> Visible on Product View Page on Front-end -> Select "YES"
Does this solve your problem?

Iptrack Magento

I use Iptrack extension in Magento. Based on IP detection the following specifications will be shown for a product (if applicable);
Specifications - it is attribute for product.
if IP is from the UK or US and language is English :
Show all I- and G-specifications
other IP's and languages :
Show all M- and G-specifications
This is where attributes come: attributes.phtml:
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct()
?>
<?php if($_additional = $this->getAdditionalData()): ?>
<h2><?php echo $this->__('Additional Information') ?></h2>
<table class="data-table" id="product-attribute-specs-table">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional as $_data): ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
<?php endif;?>
dumping $_data inside foreach give me like
array(3) { ["label"]=> string(19) "Diameter D (inches)" ["value"]=> string(7) "2 11/16" ["code"]=> string(4) "i062" } Diameter D (inches)
for one loop.
Maybe someone got the same problem? Thank you.
If I understand you correctly, you can achieve what you want by something like this:
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
function isAllowedAttribute($aData)
{
$bIpCheck = (bool) preg_match('/^[igm]{1}[0-9]{3}$/', $aData['code']);
if (!$bIpCheck) {
return true;
}
if (isIpFromUkOrUsAndLanguageEnglish()) {
return (bool) preg_match('/^[ig]{1}[0-9]{3}$/', $aData['code']);
}
else {
return (bool) preg_match('/^[mg]{1}[0-9]{3}$/', $aData['code']);
}
}
?>
<?php if($_additional = $this->getAdditionalData()): ?>
<h2><?php echo $this->__('Additional Information') ?></h2>
<table class="data-table" id="product-attribute-specs-table">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional as $_data): ?>
<?php if (isAllowedAttribute($_data)): ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
<?php endif;?>
I don't know this IP track extension you use at all, so I simply used a dummy call to a non-existing function named isIpFromUkOrUsAndLanguageEnglish().
You only need to replace this by the proper IP track extension call(s).

Resources