Created
December 6, 2018 14:31
-
-
Save dimobelov/ca9447df335bdb47d6ee775ee28bf6de to your computer and use it in GitHub Desktop.
Allow BGN for WooCommerce and PayPal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// allow BGN for WooCommerce and PayPal | |
add_filter( 'woocommerce_paypal_supported_currencies', 'add_bgn_paypal_valid_currency' ); | |
function add_bgn_paypal_valid_currency( $currencies ) { | |
array_push ( $currencies , 'BGN' ); | |
return $currencies; | |
} | |
// Convert BGN to EUR for PayPal payments | |
add_filter('woocommerce_paypal_args', 'convert_bgn_to_eur'); | |
function convert_bgn_to_eur($paypal_args){ | |
if ( $paypal_args['currency_code'] == 'BGN'){ | |
$convert_rate = 1.955; //set the converting rate | |
$paypal_args['currency_code'] = 'EUR'; //change BGN to EUR | |
$i = 1; | |
while (isset($paypal_args['amount_' . $i])) { | |
$paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2); | |
++$i; | |
} | |
if ( $paypal_args['shipping_1'] > 0 ) { | |
$paypal_args['shipping_1'] = round( $paypal_args['shipping_1'] / $convert_rate, 2); | |
} | |
if ( $paypal_args['discount_amount_cart'] > 0 ) { | |
$paypal_args['discount_amount_cart'] = round( $paypal_args['discount_amount_cart'] / $convert_rate, 2); | |
} | |
} | |
return $paypal_args; | |
} | |
//this runs when a new note is added to the order | |
add_filter( 'woocommerce_new_order_note_data', 'pbte_fix_order_status', 10, 2 ); | |
//if the note says that the PayPal currencies or amounts do not match, then we will change the status to processing | |
function pbte_fix_order_status($a_note, $a_order) | |
{ | |
//the check is done in two languages | |
if ( strpos($a_note['comment_content'],'PayPal валутите не съвпадат') !== false | |
|| strpos($a_note['comment_content'],'PayPal currencies do not match') !== false | |
|| strpos($a_note['comment_content'],'PayPal наличността не отговаря') !== false | |
|| strpos($a_note['comment_content'],'PayPal amounts do not match') !== false ) | |
{ | |
//we create the order var | |
$order = new WC_Order($a_order['order_id']); | |
//if the current status is on-hold - we change it to processing and add an optional note | |
if($order->status == 'on-hold') | |
$order->update_status('processing', 'The PayPal BGN support plugin did this note.'); | |
} | |
return $a_note; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment