我正在建立一家零售公司,利用opencart在互联网上销售黄金。不同之处在于,我只接受比特币(BTC),一种新的加密货币。
问题是贵金属价格和BTC/GBP汇率波动很大。因此,我需要我的opencart价格以相当频繁的速度自动更新,以避免通过移动汇率和亏本卖出而被发现。
我写了一个脚本,上面附有黄金、白银和白金的价格,然后用比特币标出价格。
因此,对于网站上的每一个金属项目,我希望目录中所有产品的价格都能自动变化(每15分钟一次)。
我的问题是:如何根据脚本更新opencart数据库中的价格?
价格必须根据四件事情来改变:贵重金属的种类。该项目的百分比标记,该项目所含贵金属的重量。这个项目的硬币类型。
我希望这些东西是opencart中可编辑的属性。对于每一项,都有四个(额外的)属性
金属型=>黄金=>银=>白金=>不是金属(对于其他项目,它会忽略所有重量,价格变化)百分比标记百分比(整数?)所以,假设我卖的是一枚四分之一盎司的金币,Krugerrand类型的:
四分之一盎司的金币重7.77克,因此在产品页面上,重量将与该数字一起列出。基准价格将设置为7.77克*黄金价格(这个数字是使用附加的php脚本获取的)。
我默认的黄金批发价格是10%,基础价格会上涨10%,这也会出现在产品页面上。
由于历史原因,Krugerrands的价值往往高于黄金批发价。因此,硬币的标记将是大约5比特币,价格将提高,以反映这一点。“买家不应该看到这些价格摆设,他们应该看到价格的变化来反映所有这些。”
每小时,所有产品的价格都会发生变化,以反映黄金价格的变化,以及BTC的英镑汇率。
下面是一个PHP脚本,用于提取黄金价格、白银价格和白金价格,然后在比特币中标出一克金属的价格。这是我第一次编程,所以可能有点冗长,或者没有遵循既定的约定。答案可能很复杂,所以我不指望有一个完整的答案。不过,收到一些指点会很好!
<?php
/* This is a script that ultimately displays the price of gold in grams, denominated in Bitcoins (BTC), a non fiat cryptographic currency. This is also the first thing I have ever programmed, so be nice */
// line break variable
$br = "<br> <br>";
// CURL begin
//curl session 1: grab metal prices
$ch1 = curl_init();
curl_setopt( $ch1, CURLOPT_URL, 'http://drayah.no.de/metals/latest' );
curl_setopt( $ch1, CURLOPT_RETURNTRANSFER, 1 );
$metalsdata = curl_exec( $ch1 );
// curl session 2: Grab exchange rate for BTC/GBP
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://mtgox.com/api/1/BTCGBP/public/ticker' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt( $ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt( $ch, CURLOPT_TIMEOUT_MS, 1000);
curl_setopt( $ch, CURLOPT_USERAGENT, 'useragent');
$btcgbpdata = curl_exec( $ch );
curl_close( $ch );
// curl session 3: Grab British pounds to USD exchange rates
$ch1 = curl_init();
curl_setopt( $ch1, CURLOPT_URL, 'https://raw.github.com/currencybot/open-exchange-rates/master/latest.json' );
curl_setopt( $ch1, CURLOPT_Rcheck if my code isETURNTRANSFER, 1 );
$currencydata = curl_exec( $ch1 );
//gold digest: turn JSON data into readable php, and extract the gold price
$metals = json_decode( $metalsdata, true, 512 );
$goldprice = $metals['gold']['quote'];
$silverprice = $metals['silver']['quote'];
$platprice = $metals['platinum']['quote'];
//BTC digest of JSON
$btcgbp = json_decode( $btcgbpdata, true, 512 );
$btcgbpvwap = ($btcgbp['return']['vwap']['value']);
echo "$btcgbpvwap pounds per BTC";
echo $br;
//currency digest of JSON
$currency = json_decode( $currencydata, true, 512 );
$usdgbp = ($currency['rates']['GBP']);
echo "$usdgbp pence per USD dollar";
echo $br;
echo $br;
// convert metals into price per ounce in pounds
$goldpounds = $goldprice * $usdgbp;
$silverpounds = $silverprice * $usdgbp;
$platpounds = $platprice * $usdgbp;
echo "GBP $goldpounds per ounce of gold";
echo $br;
echo "GBP $silverpounds per ounce of silver";
echo $br;
echo "GBP $platpounds per ounce of platinum";
echo $br;
echo $br;
//metals prices in grams
$goldpoundsgram = $goldpounds / 31.1034768;
$silverpoundsgram = $silverpounds / 31.1034768;
$platpoundsgram = $platpounds / 31.1034768;
echo "$goldpoundsgram pounds per gram of gold";
echo $br;
echo "$silverpoundsgram pounds per gram of silver";
echo $br;
echo "$platpoundsgram pounds per gram of platinum";
echo $br;
echo $br;
// metal prices denominated in BTC
$btcgoldgram = ($goldpoundsgram / $btcgbpvwap);
$btcsilvergram = ($silverpoundsgram / $btcgbpvwap);
$btcplatgram = ($platpoundsgram / $btcgbpvwap);
echo "<b>";
echo $btcgoldgram;
echo " bitcoins per gram of gold";
echo $br;
echo $btcsilvergram;
echo " bitcoins per gram of silver";
echo $br;
echo $btcplatgram;
echo " bitcoins per gram of platinum";
?>发布于 2012-04-10 05:56:11
实际上,price只是表product中的一个数字字段,您可以使用基于product_id的简单update语句来更新它。
同一表中还有一个列cost,用于在管理面板中报告收入。如果你对这样的报告感兴趣,你也可以更新。
要恢复product_id,可以使用product_description中的产品名称(注意名称是本地化的)。假设您的数据库包含名为克银的产品,那么您的更新可能是
update product p,product_description d set price=$btcsilvergram
where p.product_id=d.product_id and name='gram of silver'价值是免税的。
发布于 2012-04-16 02:37:54
我在看一些类似的东西。由于我的供应商价格都是以美元计价(也就是说,他们都不会接受比特币),我更愿意将这些价格硬编码到Opencart中,然后使用Opencart的内置汇率转换器来收取足够的比特币费用,以支付我在任何一天的费用。通过调整我的管理和主题设置,我只能在BTC中显示价格,并取消访问者更改默认货币(菜单项、模块等)的能力。然后,我只需要定期更新Opencart中的美元->BTC汇率,所有产品的价格都会相应地更新--每天,4倍,每小时,等等。
Opencart数据库有一个名为“货币”的表,用于存储适当的汇率。
比特币通常是以比特币-美元($XXusd每1美元),但我需要美元-比特币利率,因为我的价格存储在美元。我倒置了一个比特币价格样本(1 /4美元=> .25btc /1美元),并将其存储在Opencart中。
CURRENCY:
(4, 'Bitcoin', 'btc', 'B⃦', 'BTC', '2', 0.25000000, 1, '2012-04-10 12:13:45'),
(2, 'US Dollar', 'USD', '$', '', '2', 1.00000000, 1, '2012-04-10 12:12:33');现在,我只是一个脚本,随时更新".25000000“值。我计划避开整个Opencart系统,在服务器上设置一个cron作业,每天更新一次。如果比特币价格大幅下跌,我会在其他地方设置几个警报通知我。如果发生了一些极端的价格变化,我可以登录Opencart并手动调整利率。
希望这会有所帮助:)
https://stackoverflow.com/questions/10082201
复制相似问题