cancel
Showing results for 
Search instead for 
Did you mean: 

How to call API only one time and using response in each product block

How to call API only one time and using response in each product block

I override the template \vendor\magento\module-catalog\view\base\templates\product\price\amount\default.phtml to display a custom info in product price block. This info has been returned from an API.

My template: \app\code\MyVendor\My Module\view\frontend\templates\product\price\amount\default.phtml

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

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

<span class="price-container <?= $block->escapeHtmlAttr($block->getAdjustmentCssClasses()) ?>"
        <?= $block->getSchema() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : '' ?>>
    <?php if ($block->getDisplayLabel()) :?>
        <span class="price-label"><?= $block->escapeHtml($block->getDisplayLabel()) ?></span>
    <?php endif; ?>
    <span <?php if ($block->getPriceId()) :?> id="<?= $block->escapeHtmlAttr($block->getPriceId()) ?>"<?php endif;?>
        <?= ($block->getPriceDisplayLabel()) ? 'data-label="' . $block->escapeHtmlAttr($block->getPriceDisplayLabel() . $block->getPriceDisplayInclExclTaxes()) . '"' : '' ?>
        data-price-amount="<?= $block->escapeHtmlAttr($block->getDisplayValue()) ?>"
        data-price-type="<?= $block->escapeHtmlAttr($block->getPriceType()) ?>"
        class="price-wrapper <?= $block->escapeHtmlAttr($block->getPriceWrapperCss()) ?>"
    ><?= $block->escapeHtml($block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()), ['span']) ?></span>
    <?php if ($block->hasAdjustmentsHtml()) :?>
        <?= $block->getAdjustmentsHtml() ?>
    <?php endif; ?>
    <?php if ($block->getSchema()) :?>
        <meta itemprop="price" content="<?= $block->escapeHtmlAttr($block->getDisplayValue()) ?>" />
        <meta itemprop="priceCurrency" content="<?= $block->escapeHtmlAttr($block->getDisplayCurrencyCode()) ?>" />
    <?php endif; ?>
</span>
<?php $customInfo = $block->getCustomInfo(); //Get info from the API ?> <span><?=$customInfo?></span>

My block class is placed at \app\code\MyVendor\MyModule\Block

The problem is the API being called many times (same with the number of the product blocks). Because the result is the same at all so i just want to call API one time.

Please suggest me how can i do it.

1 REPLY 1

Re: How to call API only one time and using response in each product block

Just like optimizing API calls can save system resources, managing time effectively is crucial for personal productivity. If you're working on project timelines, development milestones, or deployment schedules, you can find ourcalculadora de datas - supercalendário useful for planning and calculating important date ranges.

By combining smart development practices with effective time management, you ensure better performance and more predictable results—both in code and in planning.