Skip to content

[Question] How to get the currency code and symbol of an AbstractPrice? #941

@Vinai

Description

@Vinai

During website implementations I often had to access currency information like the currency symbol of a price (e.g. for custom API resources, or when rendering a price on a graphical banner).

For that purpose the currency information for a given AbstractPrice instance should be made accessable for the current locale.
Currently this information is hidden 4 levels away:

  • Pricing\Price\AbstractPrice has a private Magento\Directory\Model\PriceCurrency
  • PriceCurrency gives access to Magento\Directory\Model\Currency via the public getCurrency()
  • Currency has a private Magento\Framework\Locale\Currency
  • Locale\Currency uses a CurrencyFactory to instantiate a Magento\Framework\Currency which is exposed via getCurrency() but requires a currency code as an argument.
  • Magento\Framework\Currency finally extends Zend_Currency and thus implements getSymbol()

Is there already a good way to access that information, for example in a price template?

If not, I think the cleanest way to access the data would be via getter methods on the AbstractPrice instances.
Ideally a method getCurrencySymbol would be added to the Magento\Framework\Pricing\Price\PriceInterface and implemented in AbstractPrice.

Would that be something you generally agree with?

Metadata

Metadata

Assignees

Labels

Issue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentimprovement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions