Acidburns
Acidburns

Reputation: 143

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

I'm using custom bank gateway that accept only "Macedonian denar" "MKD" - currency.

Because I'm using 3 more currencies in WooCommerce, i'm using this code:

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

EUR currency and every other currency are converted (multiplied) * 62 after I "Place Order"

For example:

But if the currency on the order page is USD $, the exchange rate for USD/MKD is * 50 , using the above code, 50 USD are converted same as EUR.


How can i upgrade the function/filter to get the currency first from the order object? like:

Upvotes: 1

Views: 404

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29624

"How can i upgrade the function/filter to get the currency first from the order object?"

It seems that the plugin you are using has not received any updates for quite some time. So asking the plugin developers to provide this does not immediately seem like an option.

The solution I am writing here is one that is normally strongly discouraged, because the changes will be lost if the plugin receives an update, but this seems unlikely.

So to answer your question

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

Replace (line 302)

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

With

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

And then you can apply the following code, via the halk_amount_fix filter hook

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 );

Another option is to directly specify all the code in the file, so that the hook is no longer applicable.

Upvotes: 1

Related Questions