在Woocommerce中显示多种货币价格

时间:2017-12-12 04:01:18

标签: php wordpress woocommerce currency cart

我正在尝试显示多种货币,

就像我们将在后端给出美元价格,但它会以比特币换算为主要价格,然后是价格,然后是欧元价格,这是我正在使用的代码....

function convertCurrency($amount, $from, $to){
    $data = file_get_contents("https://finance.google.com/finance/converter?a=$amount&from=$from&to=$to");
    preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
    $converted = preg_replace("/[^0-9.]/", "", $converted[1]);
    return number_format(round($converted, 3),2);
}

add_filter( 'wc_price', 'my_custom_price_format', 10, 4 );
function my_custom_price_format( $formatted_price, $price, $args, $unformatted_price ) {
     $price_eur = convertCurrency($price, 'USD','EUR');
    $formatted_price_eur = "<br><span class='price-eur'> (&euro;$price_eur)</span>";
    $rate_source = 'CoinDesk';
    // The currency conversion custom calculation function
    $price_btc = $value = WCR_Bitcoin_Exchange_Rate::get( $price, 'USD', 'BTC', $rate_source );
    // the currency symbol for US dollars
   $currency_symbol = '<i class="fa fa-btc"></i> ';
    $price_btc = $currency_symbol.$price_btc; // adding currency symbol
    //Bitcoin formattd price
    $formatted_price_btc = "<br><span class='price-btc'> $price_btc</span>";
    // The USD formatted price
    $formatted_price = '<br>('.$formatted_price .')';
    // Return both formatted currencies
    return $formatted_price_btc . $formatted_price . $formatted_price_eur ;
}

但我只是将美元价格调为好,但其他价格为零。当我回复$ price它显示我的双倍价格看截图:

double price

请告诉我哪里出错了。

(有关信息:WCR_Bitcoin_Exchange_Rate :: get()已经过测试并已有效)

2 个答案:

答案 0 :(得分:1)

由于我无法通过WCR_Bitcoin_Exchange_Rate::get()获得比特币汇率,因此我评论并添加了手动汇率。

您的代码中有2个错误。这个钩子函数只接受3个第一个参数。第四个不存在。

还有一个小错误:$price_btc = $value = WCR_Bitcoin_Exchange_Rate::get(… …);您可以删除= $value

我已经测试了您的更正后的代码,它只对我有用:

function convertCurrency($amount, $from, $to){
    $data = file_get_contents("https://finance.google.com/finance/converter?a=$amount&from=$from&to=$to");
    preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
    $converted = preg_replace("/[^0-9.]/", "", $converted[1]);
    return number_format(round($converted, 3),2);
}

add_filter( 'wc_price', 'my_custom_price_format', 10, 3 );
function my_custom_price_format( $formatted_price, $price, $args ) {
    ## Euros
    $price_eur = convertCurrency($price, 'USD','EUR');
    $formatted_price_eur = "<br><span class='price-eur'> (&euro;$price_eur)</span>";

    ## Bitcoin
    $rate_source = 'CoinDesk';
    // The currency conversion custom calculation function
    $price_btc = $price/15000; // WCR_Bitcoin_Exchange_Rate::get( $price, 'USD', 'BTC', $rate_source );
    // the currency symbol for BTC
    $currency_symbol = '<i class="fa fa-btc"></i> ';
    $price_btc = $currency_symbol.$price_btc; // adding currency symbol
    //Bitcoin formattd price
    $formatted_price_btc = "<br><span class='price-btc'> $price_btc</span>";

    ## USD (formatted price)
    $formatted_price = '<br>('.$formatted_price .')';

    // Return the 3 formatted currencies
    return $formatted_price_btc . $formatted_price . $formatted_price_eur ;
}

我在购物车页面中将以下内容添加到购物车中

enter image description here

答案 1 :(得分:0)

好的,这是我做的,我得到了所有设置:)

function convertCurrency($amount, $from, $to){
    $data = file_get_contents("https://finance.google.com/finance/converter?a=$amount&from=$from&to=$to");
    preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
    $converted = preg_replace("/[^0-9.]/", "", $converted[1]);
    return number_format(round($converted, 3),2);
}

add_filter( 'woocommerce_get_price_html', 'my_custom_price_format', 10, 2 );
function my_custom_price_format( $price, $product) {

     $newprice = $product->price;
    ## Euros
    $price_eur = convertCurrency($newprice, 'USD','EUR');
    $formatted_price_eur = "<br><span class='price-eur'> (&euro;$price_eur)</span>";

    ## Bitcoin
    $rate_source = 'CoinDesk';
    // The currency conversion custom calculation function
     $price_btc = WCR_Bitcoin_Exchange_Rate::get( $newprice, 'USD', 'BTC', $rate_source );
    // the currency symbol for BTC
    $currency_symbol = '<i class="fa fa-btc"></i> ';
    $price_btc = $currency_symbol.$price_btc; // adding currency symbol
    //Bitcoin formattd price
    $formatted_price_btc = "<br><span class='price-btc'> $price_btc</span>";

    ## USD (formatted price)
    $formatted_price = '<br>('.$price .')';

    // Return the 3 formatted currencies
    return $formatted_price_btc . $formatted_price . $formatted_price_eur ;
}

我刚刚更改过滤器woocommerce_get_price_html并修复了....