Skip to content

Instantly share code, notes, and snippets.

@PGeorgiev
Created January 21, 2019 08:06
Show Gist options
  • Save PGeorgiev/847daa241e0fef7f8574cfaa57e940c6 to your computer and use it in GitHub Desktop.
Save PGeorgiev/847daa241e0fef7f8574cfaa57e940c6 to your computer and use it in GitHub Desktop.
Alow BGN for WooCommerce and PayPal
// 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