0

I have a piece of code that I need to use multiple times, but I don't want to get a very long page with a lot of the same code.

I have this piece of code:

<div class="av-product-specs-list-container">
    <input type="checkbox" id="chck1" checked>
    <label class="av-product-specs-title" for="chck1">Algemene informatie</label>
    <div class="av-product-specs-table-list first-spec-list">

        <?php if( $av_merk != "" ) : ?>
            <div class="av-product-specs-table-row">
                <div class="av-product-specs-table-row-left">Merk</div>
                <div class="av-product-specs-table-row-right">
                    <?php echo $av_merk; ?>
                </div>
            </div>
        <?php endif; ?>

        <?php 
            $av_attribute_group = $_product->getAttributes(37);

            foreach ($av_attribute_group as $av_attribute_groups):

                $av_attr_name = $av_attribute_groups->getName();
                $av_attr_label = $_product->getResource()->getAttribute($av_attr_name)->getFrontend()->getLabel($_product);
                $av_attr_value = $_product->getResource()->getAttribute($av_attr_name)->getFrontend()->getValue($_product);

                if( $av_attr_value != "" ) :

                ?>

                    <div class="av-product-specs-table-row">
                        <div class="av-product-specs-table-row-left"><?php echo $av_attr_label; ?></div>
                        <div class="av-product-specs-table-row-right">
                            <?php 
                                if( $av_attr_value == "Yes" || $av_attr_value == "Ja" ) : ?>
                                    <div class="av-spec-yes"></div>
                                <?php elseif( $av_attr_value == "No" || $av_attr_value == "Nee" ) : ?>
                                    <div class="av-spec-no"></div>
                                <?php else : 
                                    echo $av_attr_value;
                                endif; 
                            ?>
                        </div>
                    </div>

                <?php endif; ?>             
        <?php endforeach ?>

    </div>
</div>

Instead of creating this piece of code over 40 times, I need to create a function that I can call and only change the $av_attribute_group = $_product->getAttributes(37); because I need to lead different id's.

How can I do that?

I've got this piece of code in StringHelper.php to load it like this:

<?php echo $this->helper('Avnedspecs\Showspecs\Helper\StringHelper')->avned_specs('37') ?>

but then I get the message that the $_product is a undefined variable... How can I fix this?

<?php

namespace Avnedspecs\Showspecs\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class StringHelper extends AbstractHelper
{

    function avned_specs($id)
    { ?>
        <div class="av-product-specs-list-container">
            <input type="checkbox" id="chck1" checked>
            <label class="av-product-specs-title" for="chck1">Algemene informatie</label>
            <div class="av-product-specs-table-list first-spec-list">

                <?php 
                    $av_attribute_group = $_product->getAttributes($id);

                    foreach ($av_attribute_group as $av_attribute_groups):

                        $av_attr_name = $av_attribute_groups->getName();
                        $av_attr_label = $_product->getResource()->getAttribute($av_attr_name)->getFrontend()->getLabel($_product);
                        $av_attr_value = $_product->getResource()->getAttribute($av_attr_name)->getFrontend()->getValue($_product);

                        if( $av_attr_value != "" ) :

                        ?>

                            <div class="av-product-specs-table-row">
                                <div class="av-product-specs-table-row-left"><?php echo $av_attr_label; ?></div>
                                <div class="av-product-specs-table-row-right">
                                    <?php 
                                        if( $av_attr_value == "Yes" || $av_attr_value == "Ja" ) : ?>
                                            <div class="av-spec-yes"></div>
                                        <?php elseif( $av_attr_value == "No" || $av_attr_value == "Nee" ) : ?>
                                            <div class="av-spec-no"></div>
                                        <?php else : 
                                            echo $av_attr_value;
                                        endif; 
                                    ?>
                                </div>
                            </div>

                        <?php endif; ?>
                <?php endforeach ?>

            </div>
        </div>
    <?php }

}

Your Answer

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

Browse other questions tagged or ask your own question.