1

In my Magento site, we found there was configuration in admin.

Admin -> Store -> Configuration -> Advanced -> Developer (Enabled only on Developer mode). JavaScript settings -> Move JS code to the bottom of the page

When this is YES, all the scripts tags on the site moves to the bottom of the page.

But I need to include a script which shouldn't move to the bottom.

My scripts loads in via .phtml. And I need to load avoid that particular script from moving to bottom

enter image description here

this is code from magento/module-bundle/view/frontend/templates/catalog/product/view/summary.phtml i have issue product summary on bundle product not showing

Is there any way to achieve this?

2

3 Answers 3

0

If you look at vendor/magento/module-theme/Controller/Result/JsFooterPlugin.php you see the regex.

It seems if you would add type or id to script tag it will exclude your script from moving to the place before closing body tag. Other option would be to modify this class.

Question is how you are adding exactly your JS that you want to exclude?

7
  • add id or type to script tag not working
    – Egi
    Aug 7, 2020 at 3:10
  • can you show some code? how you added and where? Aug 7, 2020 at 3:53
  • i have just update my question
    – Egi
    Aug 7, 2020 at 4:01
  • Those examples shouldn't change anything when moved to the bottom. What side effect it has that you don't want it to move to the bottom ? Aug 7, 2020 at 12:44
  • in another project script not move to the bottom, so i assumse issue related that. it caused summary information not showing ibb.co/sCJHTsP (ss project script not move) ibb.co/n6GJMcN (ss project script move bottom)
    – Egi
    Aug 8, 2020 at 1:48
0

so in the end, i choose to create mixin js for product-summary.js

define([
    'jquery',
    'mage/template',
    'jquery-ui-modules/widget',
    'Magento_Bundle/js/price-bundle'
], function ($, mageTemplate) {
    'user strict';

    return function(productSummary) {
        $.widget('mage.productSummary', productSummary, {
            _renderOption: function (key, row) {
                var template;
                if (row && row.length > 0 && row[0] !== null) {
                    template = $('[data-template="bundle-summary"]').html();
                    template = mageTemplate($.trim(template), {
                        data: {
                            _label_: this.cache.currentElement.options[key].title
                        }
                    });
                    this.cache.currentKey = key;
                    this.cache.summaryContainer = $(template);
                    this.element.append(this.cache.summaryContainer);
                    $.each(row, this._renderOptionRow.bind(this));
                    this.cache.currentElementCount += row.length;
                    //Reset Cache
                    this.cache.currentKey = null;
                }
            },

            _renderOptionRow: function (key, optionIndex) {
                var template;
                template = $('[data-template="bundle-option"]').html();
                template = mageTemplate($.trim(template), {
                    data: {
                        _quantity_: this.cache.currentElement.options[this.cache.currentKey].selections[optionIndex].qty,
                        _label_: this.cache.currentElement.options[this.cache.currentKey].selections[optionIndex].name
                    }
                });
                this.cache.summaryContainer
                    .find(this.options.optionSelector)
                    .append(template);

            }
        });
        return $.mage.productSummary;
    }
});

and don't forget add config to requirejs-config.js

cheers :)

0

Two options:

  1. Override entire controller method - beforeSendResponseMake in Magento\Theme\Controller\Result\JsFooterPlugin

  2. Add extra tag in your script tag to skip the regex

    <script[^>]*+(?<!text/x-magento-template.)>. *?</script>

So just add "text/x-magento-template" in your script tag. Make sure you dont use type="" attribute, and make sure its the last attribute without any space after, eg:

<script src="xyz.js" exclude-this-tag="text/x-magento-template"></script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.