在 Woocommerce 的 Halk Bank 支付网关中将订单总额转换为默认货币

Convert order total to default currency in Halk Bank Payment Gateway For Woocommerce

我使用的自定义银行网关只接受“马其顿第纳尔”“MKD”货币。

因为我在 WooCommerce 中使用了 3 种以上的货币,所以我使用此代码:

add_filter( 'halk_amount_fix', function( $total ) { return $total * 62; } );

在我“下订单”后,欧元货币和其他所有货币都转换(相乘)* 62

例如:

但是如果下单页面的币种是USD$,那么USD/MKD的汇率是*50,用上面的代码,50美元和欧元一样换算。


如何升级 function/filter 以首先从订单对象中获取货币?喜欢:

"如何升级 function/filter 以首先从订单对象中获取货币?"

您使用的插件似乎已经有一段时间没有收到任何更新了。所以要求插件开发者提供这个似乎不是一个立即的选择。

我在这里写的解决方案通常是强烈反对的,因为如果插件收到更新,更改将会丢失,但这似乎不太可能。

所以回答你的问题

woo-halkbank-payment-gateway/classes/class-wc-halk-payment-gateway.php

替换(第 302 行)

$amount = number_format( apply_filters( 'halk_amount_fix', $order->get_total() ),  2, '.', '' );  //Transaction amount

$amount = apply_filters( 'halk_amount_fix', number_format( $order->get_total(),  2, '.', ''), $order );  //Transaction amount

然后您可以通过 halk_amount_fix 过滤器挂钩

应用以下代码
function filter_halk_amount_fix( $amount, $order ) {            
    // Get currency
    $currency_code = $order->get_currency();
        
    // Compare
    if ( $currency_code == 'USD' ) {
        return number_format( $amount * 50, 2, '.', '' );
    } elseif ( $currency_code == 'EUR' ) {
        return number_format( $amount * 62, 2, '.', '' );
    } elseif ( $currency_code == 'GBP' ) {
        return number_format( $amount * 45, 2, '.', '' );
    }
    
    return $amount;
}
add_filter( 'halk_amount_fix', 'filter_halk_amount_fix', 10, 2 );

另一种选择是直接指定文件中的所有代码,这样hook就不再适用了。