0

How do I retrieve the product price in form.phtml and assign it to an HTML attribute data-purchase-amount ="" as code snippet meant for Klarna tagline placement as seen in the file below.

<div class="product-add-form">
    <form data-product-sku="<?= $block->escapeHtmlAttr($_product->getSku()) ?>"
          action="<?= $block->escapeUrl($block->getSubmitUrl($_product)) ?>" method="post"
          id="product_addtocart_form"<?php if ($_product->getOptions()) :?> enctype="multipart/form-data"<?php endif; ?>>
        <input type="hidden" name="product" value="<?= (int)$_product->getId() ?>" />
        <input type="hidden" name="selected_configurable_option" value="" />
        <input type="hidden" name="related_product" id="related-products-field" value="" />
        <input type="hidden" name="item"  value="<?= (int)$block->getRequest()->getParam('id') ?>" />
        <?= $block->getBlockHtml('formkey') ?>
        <?= $block->getChildHtml('form_top') ?>
        <?php if (!$block->hasOptions()) :?>
            <?= $block->getChildHtml('product_info_form_content') ?>
        <?php else :?>
            <?php if ($_product->isSaleable() && $block->getOptionsContainer() == 'container1') :?>
                <?= $block->getChildChildHtml('options_container') ?>
            <?php endif;?>
        <?php endif; ?>

        <?php if ($_product->isSaleable() && $block->hasOptions() && $block->getOptionsContainer() == 'container2') :?>
            <?= $block->getChildChildHtml('options_container') ?>
        <?php endif;?>
        <?= $block->getChildHtml('form_bottom') ?>
    </form>
</div>
<div class="klarna-placement">
<!-- Placement v2 -->
<klarna-placement
  data-key="credit-promotion-badge"
  data-locale="en-US"
  data-purchase-amount=""
></klarna-placement>
<!-- end Placement -->
</div>

1 Answer 1

0

You have product object already. Try following simple way:

<?= $_product->getFinalPrice() ?>

[Update]

After adding following code snippet:

<div class="klarna-placement">
    <!-- Placement v2 -->
    <klarna-placement
        data-key="credit-promotion-badge"
        data-locale="en-US"
        data-purchase-amount="<?= $_product->getFinalPrice() ?>"
    ></klarna-placement>
    <!-- end Placement -->
</div>

I can see the price: enter image description here

5
  • thanks a lot. it is returning the exact price when I echo it, not when I used your line of code. Please advise as to how to use it here data-purchase-amount=<?= $_product->getFinalPrice() ?> May 16, 2020 at 9:39
  • This is what I am trying: <klarna-placement data-key="credit-promotion-badge" data-locale="en-US" data-purchase-amount="<?= $_product->getFinalPrice() ?>" ></klarna-placement> May 16, 2020 at 9:47
  • Check the updated answer. I have tested with latest version.
    – Sohel Rana
    May 16, 2020 at 9:49
  • Yes,the tag considers only integer value. I need to now find a way to convert it into integer. Any advise please? May 16, 2020 at 9:51
  • Sohel, is there a way to get the product price as it is exactly? For example, if the price is 458.90, I want the function to return exactly that unlike the function above that returns only 458.9? May 16, 2020 at 10:18

Your Answer

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

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