1

I want to make javascript at the end of the file run only if the product is not from the "hello-kitty" manufacturer but for that I need to somehow take the manufacturer of the product to store in a variable and put it in javascript condition.How i do that?

FIle: vendor/magento/module-catalog/view/base/templates/product/price/amount/default.phtml

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

    // @codingStandardsIgnoreFile

    ?>

    <?php /** @var \Magento\Framework\Pricing\Render\Amount $block */ ?>

    <span class="price-container <?= /* @escapeNotVerified */ $block->getAdjustmentCssClasses() ?>"
            <?= $block->getSchema() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : '' ?>>
        <?php if ($block->getDisplayLabel()): ?>
            <span class="price-label"><?= /* @escapeNotVerified */ $block->getDisplayLabel() ?></span>
        <?php endif; ?>
        <span <?php if ($block->getPriceId()): ?> id="<?= /* @escapeNotVerified */ $block->getPriceId() ?>"<?php endif;?>
            <?= ($block->getPriceDisplayLabel()) ? 'data-label="' . $block->getPriceDisplayLabel() . $block->getPriceDisplayInclExclTaxes() . '"' : '' ?>
            data-price-amount="<?= /* @escapeNotVerified */ $block->getDisplayValue() ?>"
            data-price-type="<?= /* @escapeNotVerified */ $block->getPriceType() ?>"
            class="price-wrapper <?= /* @escapeNotVerified */ $block->getPriceWrapperCss() ?>"
        ><?= /* @escapeNotVerified */ $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) ?></span>
        <?php if ($block->hasAdjustmentsHtml()): ?>
            <?= $block->getAdjustmentsHtml() ?>
        <?php endif; ?>
        <?php if ($block->getSchema()): ?>
            <meta itemprop="price" content="<?= /* @escapeNotVerified */ $block->getDisplayValue() ?>" />
            <meta itemprop="priceCurrency" content="<?= /* @escapeNotVerified */ $block->getDisplayCurrencyCode() ?>" />    
    <?php endif; ?>
    </span>
    <script>
    //LongDrink
    function drink(){
            if(window.location.href == "https://www.example.com/presentes/copo-longdrink"){
                    const spans=document.querySelectorAll(".info-details span.price");
                    for(let i=0;i<spans.length;i++){
                    const span=spans[i];span.classList.add("forcehide");
                            }
                    }
            return null;
            }
    drink();
    //FimLongDrink
    </script>
0

Update your code inside <script> tag to be like below:

<?php if ('hello-kitty' !== $block->getSaleableItem()->gerResource()->getAttribute('manufacturer')->getFrontend()->getValue($block->getSaleableItem())): ?>
    <script>
        require([
            'jquery'
        ], function ($) {
            $(function () {
                if (window.location.href == "https://www.example.com/presentes/copo-longdrink") {
                    const spans = document.querySelectorAll(".info-details span.price");
                    for (let i = 0; i < spans.length; i++){
                        const span = spans[i];
                        span.classList.add("forcehide");
                    }
                }
            });
        });
    </script>
<?php endif ?>
  • But where is the condition of the manufacturer? – Richard Nicson Sep 19 at 17:33
  • please check updated answer. – Rendy Eko Prastiyo Sep 19 at 18:02
  • Rendy, Good the idea, but I inserted the code after fixing the getresource that was spelled wrong, the code appears and then erases the forcehide class is getting. Should this condition of manufacture not be within FOR so that it goes through product by product to see if the product is from manufacturer Hello-Kitty? – Richard Nicson Sep 20 at 12:52

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.