【问题标题】:Display multiple currencies prices in Woocommerce在 Woocommerce 中显示多种货币价格
【发布时间】:2017-12-12 04:01:18
【问题描述】:

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

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

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 时,它​​会显示双倍价格,请参见截图:

请让我知道哪里做错了..

(有关信息:WCR_Bitcoin_Exchange_Rate::get() 已经过测试并可以使用)

【问题讨论】:

    标签: php wordpress woocommerce currency cart


    【解决方案1】:

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

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

    还有这个小错误:$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 ;
    }
    

    对于添加到购物车的一种产品,我在购物车页面中获得以下信息:

    【讨论】:

    • 对不起,但在我这边它不起作用,即使我在另一个站点尝试过,仍然存在同样的问题:(
    • @MohammadUmer 对我来说,使用 Storefront 主题的 WooCommerce 3.2.5 它可以工作。
    • 1,599.001,599.00 所以 btc 0 和 eur 0.85
    【解决方案2】:

    好的,我做到了,我已经准备好了:)

    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 并得到了修复....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      相关资源
      最近更新 更多