cancel
Showing results for 
Search instead for 
Did you mean: 

Pulling product price to new block

Pulling product price to new block

I have added a new block to my product page. In the block i want to display the product price so I can then take that price and divide it by 3 to show monthly payments. I have tried to get the price by copying code used in other phtml files but it's not even displaying the price.

Can someone advise on what i need to do in order to get the price to show?

Here is the code i have so far in my phtml file.

<div class="klarna_calc">
    <img class="klarna_logo" src="/media/Klarna_logo_bw.svg" alt="Klarna logo"/>
    <i class="far fa-check-circle"></i>
    <p>Price 
        <?= $price = $block->escapeHtmlAttr($block->getDisplayValue()) ?>
        <?php echo $price; ?>    </p>
</div>
1 REPLY 1

Re: Pulling product price to new block

Using Object Manager(Not Recommended)

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
echo $product->getPrice();
?>

Using block

<?php

namespace Vendor\Module\Block;
class BlockClass extends \Magento\Framework\View\Element\Template
{
protected $registry;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
array $data = []
)
{
$this->registry = $registry;
parent::__construct($context, $data);
}
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function getCurrentProduct()
{
return $this->_registry->registry('current_product');
}
}
?>
Call function in your .phtml file:
<?php
$currentProduct = $block->getCurrentProduct();
echo $currentProduct->getPrice();
?>
If issue solved, Click Kudos & Accept as Solution.
LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool