4

I've custom block phtml in my old store and I need to be converted to Magento 2.

Magento 1.9.x code:

$_finalPriceInclTax = $this->helper('tax')->getPrice($_product, $_product->getFinalPrice(), true);
$unitPrice = Mage::helper('core')->currency(round($_finalPriceInclTax / $_product->getHedNetWeight(), 2),true,false);

How can I get Final Price included Tax in Magento 2 and how can I use Mage::helper('core')->currency(round) in Magento 2?

I try to display the price like this:

$this->helper('Magento\Framework\Pricing\Helper\Data')->currency($finalprice / $HedNetWeight,true,false);

Thank you

1

3 Answers 3

7

In Block :-

protected $priceCurrency;

protected $taxHelper;

public function __construct(\Magento\Catalog\Helper\Data $taxHelper,
    \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
){
    $this->taxHelper = $taxHelper;
    $this->priceCurrency = $priceCurrency;
}

public function finalPrice($product) {
    return $this->taxHelper->getTaxPrice($product, $product->getFinalPrice(), true);
}

public function currency($value, $format = true, $includeContainer = true)
{
    return $format
    ? $this->priceCurrency->convertAndFormat($value, $includeContainer)
    : $this->priceCurrency->convert($value);
}

In phtml :-

<?= $block->currency($block->finalPrice(),true,false); ?>
2
  • 1
    Hi thank you for your answer, but if you read again I need this to be just a simple code in phtml file
    – Robert
    Mar 4, 2019 at 10:38
  • 1
    check my updated answer Mar 4, 2019 at 10:51
1

Try this one

$_product->getPriceInfo()->getPrice('final_price')->getValue()
0

A couple of extensions allows to change or delete the decimal number for the front store.

https://github.com/lillik/magento2-price-decimal

https://github.com/karliuka/m2.Price

1
  • 1
    Hi thank you, but I don't need a global solution, I need to remove the decimal numbers only in my code
    – Robert
    Mar 4, 2019 at 10:20

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.