0

Does magento 2 is having something inbuilt function for currency symbol getting ?
What I want is - Input will be 3 characters currency code as USD, INR, JPY, EUR etc. corresponsing to that output should be $ , ₹ , ¥ etc...
All the solutions are appreciated even if its inbuilt in magento 2 or custom function or some API.
Edit - Code Added
At the position echo $ocd; it gives me USD after that I need to print $, it may give Euro then I need to echo € and so on..

$detail_query_1="SELECT a.quote_id,a.entity_id,a.state,a.status,a.coupon_code,a.shipping_description,a.order_currency_code,a.discount_amount,a.customer_id,a.base_discount_amount,a.base_grand_total,a.base_shipping_amount,DATE_FORMAT(a.created_at,'%d-%m-%Y') AS order_date,a.total_item_count,b.increment_id FROM rpt_sales_order AS a JOIN rpt_sales_order_grid AS b ON a.entity_id=b.entity_id WHERE 1 AND b.increment_id='$ids'";
 //echo $detail_query_1;//exit;
 $sql_detail_1=mysqli_query($con,$detail_query_1) or die(mysqli_error($con));
 if(@mysqli_num_rows($sql_detail_1))
 { 
  $cnt1=@mysqli_num_rows($sql_detail_1);
  while($rt_details_1=mysqli_fetch_array($sql_detail_1,MYSQLI_ASSOC))
  {
   #print_r($rt_details);exit;
   $ordDt=$rt_details_1['order_date']; 
   $shpDesc=$rt_details_1['shipping_description']; 
   $ocd=  $rt_details_1['order_currency_code'];
   echo $ocd;
   $discount_amount=  $rt_details_1['discount_amount'];
  }

echo '<h3 class="shipping-method">Shipping Method: '.$shpDesc.'</h3>';
 }

Thanks
Vibhore
1
  • @Mital Shah - I added the code also for your reference at echo $ocd; i need some changes after that to display the currency symbol. May 1 at 6:47
1

1. Set your symbol currency from admin settings Store -> Currency -> Currency Symbols
Currency Symbol Setup

As per you question try below code

$currency = $_objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->load($currencyCode);
$currencySymbol = $currency->getCurrencySymbol();

As per your requirement, Add this code in php file at Magento root folder and run through Magento URL. For example: You file name script.php and run magentostore.com/script.php

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
 
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
 
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
 
$currencyCode = 'your_code';
$currency = $objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->load($currencyCode);
echo $currencySymbol = $currency->getCurrencySymbol();

?>
7
  • I added the code also, please check this question again. May 1 at 6:48
  • @VibhoreJain I edit my answer, please check.
    – Mital Shah
    May 1 at 6:57
  • I appreciate your answer but May i get the equivalent code in pure php(not magento) ? I have .php file outside the magento scope.. May 1 at 7:18
  • Please keep this current code also(it is also useful) and just expand your answer to show me the code for pure php file also. May 1 at 7:21
  • 1
    For your information - I cut/paste "use and require" top 2 lines into the top of my php file that removed error 500 .. thanks :) May 1 at 8:03
0

Those who uses plain php then they can take the code from here -

$orderDetailsQuery = "SELECT DISTINCT a.sku,a.product_id,b.order_currency_code,b.entity_id,a.order_id,b.customer_email,b.increment_id,a.name,a.created_at,a.weight,a.qty_ordered,a.base_original_price,c.firstname,c.lastname,c.postcode,c.street,c.city,c.telephone,c.country_id,c.address_type,c.company,d.status,d.expected_close_time, d.couriers,d.tracking_no,e.postcode AS bill_postcode,e.street AS bill_street,e.city AS bill_city,e.country_id AS bill_country,f.cc_last_4,f.method FROM rpt_sales_order_item AS a INNER JOIN rpt_sales_order AS b ON a.order_id=b.entity_id INNER JOIN rpt_sales_order_address AS c ON b.entity_id=c.parent_id AND c.address_type='shipping' INNER JOIN rpt_sales_order_address AS e ON b.entity_id=e.parent_id AND e.address_type='billing' INNER JOIN rpt_sales_order_payment AS f ON b.entity_id=f.parent_id LEFT JOIN rpt_order_management AS d ON b.increment_id=d.order_id AND d.status='Shipped' WHERE b.increment_id=".$_POST['OrderData']."";
   
   // echo $orderDetailsQuery;
   $OrderRes = mysqli_query($conn, $orderDetailsQuery);

   while($rows = mysqli_fetch_array($OrderRes)){
      // var_dump($rows);
      $ksb_currency= $rows['order_currency_code'];

      $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
   $ksb_currency_symbol = $formatter->formatCurrency(34634, $ksb_currency);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.