-2

I developing ecommerce website. Almost i have done. But i have one problem in show product price in dollar. If user open website in india shows product price in rupees and if website access in out of country shows product price in dollar.

I created product detail page and below provide my code:

public function getProductDetail($pro_code,$error_msg){
$showProDetail='';
$result =$this->connect()->query("SELECT * FROM wm_products WHERE pro_code='$pro_code'");
if($result->num_rows>0){            
    $row = $result->fetch_assoc();
    $cate_id = $row['cate_id'];         
    $image = json_decode($row['pro_img']);
    $showProDetail.='
    <div class="page product_detail_wrapper">
        <div class="panel-body">
            <div class="row">
                <div class="col-lg-4 col-mg-4 col-sm-4 col-xs-12">
                    <figure class="thumbnail">
                        <img src="../uploads/product_images/' .$image[0].'" alt="">
                    </figure>
                </div>
                <div class="col-lg-8 col-mg-8 col-sm-8 col-xs-12">
                    <div class="responsive-table">
                        <table class="table table-striped">                             
                            <tr>
                                <td><strong>Product Code</strong></td>
                                <td>:'.$row["pro_code"].'</td>
                            </tr>
                            <tr>
                                <td><strong>Name</strong></td>
                                <td>:'.ucfirst(str_replace("_", " ", $row["pro_name"])).'</td>
                            </tr>                   
                            <tr>
                                <td><strong>Price</strong></td>
                                <td>: Rs.'.$row["pro_price"].'</td>
                            </tr>
                            <tr>
                                <td><strong>Discount</strong></td>
                                <td>:'.$row["pro_discount"].'</td>
                            </tr>
                            <tr>
                                <td><strong>Available In</strong></td>
                                <td>:'.$row["pro_weight"].'</td>
                            </tr>
                            <tr>
                                <td><strong>Quantity</strong></td>
                                <td>:'.$row["pro_quantity"].'</td>
                            </tr>
                            <tr>
                                <td><strong>Short Description</strong></td>
                                <td>:'.$row["short_disc"].'</td>
                            </tr>                                   
                        </table>
                    </div>
                </div>
            </div><br>              
        </div>
    </div>';
}
else{
    $showProDetail = '<div class="alert alert-danger">Product Detail Not Found</div>';
}
return  $showProDetail;
}

So, How to set Product price for india in rupees and out of india in dollar?

Chandan Jha
  • 45
  • 1
  • 2
  • 10
  • You can use ip to track the web user country. This question has already an answer here. https://stackoverflow.com/a/13600004/8913606 – melvin Feb 15 '19 at 08:22
  • Possible duplicate of [Getting visitors country from their IP](https://stackoverflow.com/questions/12553160/getting-visitors-country-from-their-ip) – Federico Grandi Feb 15 '19 at 10:17

2 Answers2

3

To set the product price you need to do the following 4 steps:

1. Get Visitor Location. So you can locate the visitor’s current loctaion using HTML Geolocation API. Here https://www.codexworld.com/get-visitor-location-using-html5-geolocation-api-php/ you can find the tutorial on how to Get Visitor Location using HTML5 Geolocation API in PHP.

2. Convert currency from INR to USD. Because you need to set the product price in USD for non-indian visitor's. So for converting INR to USD you can use the following source code:

function currency($amount) {
 $from_currency = "INR";
 $to_currency = "USD";
 $from_currency = urlencode($from_currency);
 $to_currency = urlencode($to_currency);
 $url = "https://www.google.com/search?q=".$from_currency."+to+".$to_currency;
 $get = file_get_contents($url);
 $data = preg_split('/\D\s(.*?)\s=\s/',$get);
 $exhangeRate = (float) substr($data[1],0,7);
 $converted_amount  = $amount*$exhangeRate;
 return $converted_amount;
}
  1. Inside the getProductDetail function's you can check the visitor's location.Above the $showProDetail statement you can check it by the following process:

    if($location == 'India') {
        $price = "Rs.".$row["pro_price"];
    } else {
        $price = "USD.".currency($row["pro_price"]);
    }
    
  2. Finally, You can directly place the product price in your price td.

    <tr>
        <td><strong>Price</strong></td>
        <td>:'.$price.'</td>
    </tr>
    
Makdia Hussain
  • 744
  • 3
  • 11
  • Getting error on your code **Warning: file_get_contents(https://www.google.com/finance/converter?a=100&from=INR&to=USD): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in** – Chandan Jha Feb 15 '19 at 08:53
  • This means you are in shared hosting & file_get_contents is blocked for you. alternatively you can use curl to fetch the result. Google it & you'll find many links to perform this. – techworld Feb 15 '19 at 10:55
1
 $amount=100; //Your amount
 $from_currency = "INR";
 $to_currency = "USD";
 $from_currency = urlencode($from_currency);
 $to_currency = urlencode($to_currency);
 $url = "https://www.google.com/search?q=".$from_currency."+to+".$to_currency;
 $get = file_get_contents($url);
 $data = preg_split('/\D\s(.*?)\s=\s/',$get);
 $exhangeRate = (float) substr($data[1],0,7);
 $converted_amount  = $amount*$exhangeRate;




please use $url = "https://www.google.com/search?q=".$from_currency."+to+".$to_currency;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459