0

I have created a module to override a product template file (attribute.phtml) and JavaScript code is printed in the frontend. I have the JavaScript code inside <script> tags, and I have also put them inside <head> tags. But the code is still printed in the frontend, and so the module isn't working.

And the strange thing is, the same code and module work perfectly for another product template file (addto.phtml).

EDIT: THE CODE IN QUESTION

vendor\Magento\module-catalog\view\frontend\templates\product\view\attribute.phtml

<?php
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */

    // @codingStandardsIgnoreFile

    /**
     * Product view template
     *
     * @see \Magento\Catalog\Block\Product\View\Description
     */
?>
<?php
    $_helper = $this->helper('Magento\Catalog\Helper\Output');
    $_product = $block->getProduct();
    $_call = $block->getAtCall();
    $_code = $block->getAtCode();
    $_className = $block->getCssClass();
    $_attributeLabel = $block->getAtLabel();
    $_attributeType = $block->getAtType();
    $_attributeAddAttribute = $block->getAddAttribute();

    $renderLabel = true;
    // if defined as 'none' in layout, do not render
    if ($_attributeLabel == 'none') {
        $renderLabel = false;
    }

    if ($_attributeLabel && $_attributeLabel == 'default') {
        $_attributeLabel = $_product->getResource()->getAttribute($_code)->getStoreLabel();
    }
    if ($_attributeType && $_attributeType == 'text') {
        $_attributeValue = ($_helper->productAttribute($_product, $_product->$_call(), $_code)) ? $_product->getAttributeText($_code) : '';
    } else {
        $_attributeValue = $_helper->productAttribute($_product, $_product->$_call(), $_code);
    }
?>
<?php if ($_attributeValue): ?>
    <div class="product attribute <?= /* @escapeNotVerified */ $_className ?>">
        <?php if ($renderLabel): ?><strong class="type"><?= /* @escapeNotVerified */ $_attributeLabel ?></strong><?php endif; ?>
        <div class="value" <?= /* @escapeNotVerified */ $_attributeAddAttribute ?>><?= /* @escapeNotVerified */ $_attributeValue ?></div>
    </div>
<?php endif; ?>
<?php
    $product = $block->getProduct();
?>
<?php if( $product->getData('size_chart') ): ?>
<?php
    $sc = $product->getData('size_chart');
    $sc = 'Mark_Sizes::documents/' . $sc . '?iframe=true';
?>
<head>
    <script>
    require(
        [
            'jquery',
            'Magento_Ui/js/modal/modal'
        ],
        function(
            $,
            modal
        ) {
            var options = {
                type: 'popup',
                responsive: true,
                innerScroll: true,
                title: $.mage.__('Sizes'),
                buttons: [{
                    text: $.mage.__('Continue'),
                    class: '',
                    click: function () {
                        this.closeModal();
                    }
                }]
            };
            var popup = modal(options, $('#popup-modal'));
            //$('#popup-modal').modal('openModal');
            $("#click-me").on('click',function(){
                $("#popup-modal").modal("openModal");
            });
        });
    </script>
</head>

<a href="#" id="click-me" class="action feature feature-icon-hover open-modal-form" title="Μεγεθολόγιο" aria-label="Μεγεθολόγιο"role="button">
    <span class="icon ib ib-hover ic ic-tshirt icon-color-productview"></span>
    <span class="label">Μεγεθολόγιο</span>
</a>

<div id="popup-modal" style="display:none;">
    <img src="<?php echo $this->getViewFileUrl($sc); ?>"/>
</div>
<?php endif; ?>

And this is how is shown in the frontend.

And this is how is shown in the frontend

5
1

Please remove <head> tag from your code.

It is not required in your vendor\Magento\module-catalog\view\frontend\templates\product\view\attribute.phtml file.

And check it.

1
  • 1
    Please don't add duplicate answers. – Ben Crook Jun 7 '18 at 10:15
1

Remove the <head> tag, this tag is already defined in /vendor/magento/module-theme/view/base/templates/root.phtml:12 - Plus it should not be a child of body.

4
  • Well the <head> tag was a solution to the same problem. The problem persists without the tag – SUPERDEVELOPER Jun 7 '18 at 10:37
  • 1
    @SUPERDEVELOPER In that case it's hard to tell, there's nothing obvious in that code that could cause it so it could be another tag that isn't closed. Does your page meet HTML standards if you run it through a validator? – Ben Crook Jun 7 '18 at 11:02
  • I only inspected it with Google dev tools and nothing showed up. I will use an HTML validator and report back with an edit. – SUPERDEVELOPER Jun 7 '18 at 11:37
  • I used this and nothing out of the ordinary/addressing my problem. It could be a tag problem, but I used the same code snippet in another template file and it worked fine. Really strange. – SUPERDEVELOPER Jun 7 '18 at 11:55
0

The <head> was an approach as solving it.

In the end I solved it by enclosing all the code under the last PHP if clause inside a div.

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.