Skip to content

Instantly share code, notes, and snippets.

@Dan0sz Dan0sz/Model\Custom.php
Last active Feb 4, 2019

Embed
What would you like to do?
How to Format Prices using the Price Currency Interface in Magento 2
<?php
namespace Daan\CustomModule\Model; // Or Controller, or Plugin, or anything actually.
use Magento\Framework\Pricing\PriceCurrencyInterface as CurrencyInterface;
class Custom {
protected $currencyInterface;
public function __construct(
CurrencyInterface $currencyInterface
) {
$this->currencyInterface = $currencyInterface;
}
// Let's imagine I passed a valid $productCollection here.
public function getMyFormattedPrice( $productCollection ) {
$prices = [];
foreach ( $productCollection as $product ) {
$prices[] = $this->currencyInterface->format(
$product->getPrice(), // the price value.
false, // don't include the container span.
4 // default precision is two decimals, but I want 4 for some reason.
);
}
return $prices;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.