0

This is the attributes.phtml located in \vendor\magento\module-catalog\view\frontend\templates\product\view which shows additional attributes:

<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
?>
<?php if ($_additional = $block->getAdditionalData()): ?>
<div class="additional-attributes-wrapper table-wrapper">
    <table class="data table additional-attributes" id="product-attribute-specs-table">
        <caption class="table-caption"><?= /* @escapeNotVerified */ __('More Information') ?></caption>
        <tbody>
        <?php foreach ($_additional as $_data): ?>
            <tr>
                <th class="col label" scope="row"><?= $block->escapeHtml($_data['label']) ?></th>
                <td class="col data" data-th="<?= $block->escapeHtml($_data['label']) ?>"><?= /* @escapeNotVerified */ $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>           
                
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>

I try to use this code for getting the attribute modell in the title.phtml located in \app\design\frontend\Company\Vendor\Magento_Theme\templates\html but it doesn't work:

if ($block->$_data['label'] = 'modell'):

The code for title.phtml is:

<?php if ($title): ?>
<div class="page-title-wrapper<?php /* @escapeNotVerified */
echo $cssClass; ?>">
    <h1 class="page-title"
        <?php if ($block->getId()): ?> id="<?php /* @escapeNotVerified */
        echo $block->getId(); ?>" <?php endif; ?>
        <?php if ($block->getAddBaseAttributeAria()): ?>
            aria-labelledby="<?php /* @escapeNotVerified */
            echo $block->getAddBaseAttributeAria(); ?>"
        <?php endif; ?>>
        <?php /* @escapeNotVerified */
        

        $_helper = $this->helper('Magento\Catalog\Helper\Output');
        $_product = $block->getProduct();
        if ($_additional = $block->getAdditionalData()):
            foreach ($_additional as $_data):
                if ($block->$_data['label'] = 'modell'): //Tried here
                    echo $block->escapeHtml($_data['label']);
                    echo $_helper->productAttribute($_product, $_data['value'], $_data['code']);
                endif;
            endforeach;
        endif;
    
        
        echo $title ?>
    </h1>
    <?php echo $block->getChildHtml(); ?>
</div>

How can I get this work? Thanks

Edit:

I want to display this attribute for the product which has the attribute_code modell

enter image description here

1

Try to replace the = in condition by ==

This doesn't make any sense in php

    if ($_additional = $block->getAdditionalData()):
        foreach ($_additional as $_data):
            if ($block->$_data['label'] = 'modell')

What you want is probably

    if ($_additional = $block->getAdditionalData()):
        foreach ($_additional as $_data):
            if ($_data['label'] == 'modell')

Also this is fine but might be miss understood

if ($_additional = $block->getAdditionalData())

Prefer

$_additional = $block->getAdditionalData();
if($_additional)
4
  • Thanks now it is working without errors but I get no result. I updates my question, can you please have a look if I can get the attribute with this query? Thanks
    – Kᴀτᴢ
    Nov 17 '20 at 14:02
  • $product->getModell() should get you the value you want from the product. Magento 2 use "magical" getter / setter. So if you have a product attribute toto you can use getToto() and setToto(....)`
    – Claims
    Nov 17 '20 at 15:51
  • I tried but gives me an error: Fatal error: Uncaught Error: Call to a member function getModell() on null
    – Kᴀτᴢ
    Nov 18 '20 at 21:16
  • That means your product is not recovered properly.
    – Claims
    Nov 19 '20 at 9:38

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.