Magento 2 : Listing component amount formatting

Posted . Visible to the public.

Formatting amount column with currency symbol

The prices column can be be formatted to two decimal and currency symbol

Following example shows how to add formatting for a amount column.

/**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $currencyCode = null;
                if (!$currencyCode) {
                    $store = $this->storeManager->getStore(
                        $this->context->getFilterParam('store_id', \Magento\Store\Model\Store::DEFAULT_STORE_ID)
                    );
                    $currencyCode = $store->getBaseCurrency()->getCurrencyCode();
                }
                $basePurchaseCurrency = $this->currency->load($currencyCode);
                $item[$this->getData('name')] = $basePurchaseCurrency
                    ->format($item[$this->getData('name')], [], false);
            }
        }

        return $dataSource;
    }
vasan
Last edit
vasan
Posted by vasan to vasan's deck (2022-03-24 16:37)