我正在开发电子商务网站。几乎我已经完成了。但我有一个问题,以美元显示产品价格。如果印度的用户打开网站显示卢比的产品价格,并且在国外的网站访问显示产品价格为美元。
我创建了产品详细信息页面,下面提供了我的代码
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;
}
那么,如何设定印度卢比和印度的产品价格?
$amount=100; //Your ammount
$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;
要设置产品价格,您需要执行以下4个步骤: 1.获取访客位置。因此,您可以使用HTML Geolocation API找到访问者的当前位置。在这里https://www.codexworld.com/get-visitor-location-using-html5-geolocation-api-php/,您可以在PHP中找到有关如何使用HTML5 Geolocation API获取访客位置的教程。 2.将货币从INR转换为美元。因为您需要为非印度游客设置美元的产品价格。因此,要将INR转换为USD,您可以使用以下源代码:
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;
}
if($location == 'India') {
$price = "Rs.".$row["pro_price"];
} else {
$price = "USD.".currency($row["pro_price"]);
}
<tr>
<td><strong>Price</strong></td>
<td>:'.$price.'</td>
</tr>