From 8847801e9e794525dc4d4828acf97b0da1053362 Mon Sep 17 00:00:00 2001 From: Dat Hoang Date: Sat, 31 Dec 2016 16:21:16 +0700 Subject: [PATCH] Add: The class to add the support for VND when using the Paypal Standard gateway - Convert VND to USD --- inc/class-wooviet-vnd-paypal-standard.php | 109 ++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 inc/class-wooviet-vnd-paypal-standard.php diff --git a/inc/class-wooviet-vnd-paypal-standard.php b/inc/class-wooviet-vnd-paypal-standard.php new file mode 100644 index 0000000..6f18f1b --- /dev/null +++ b/inc/class-wooviet-vnd-paypal-standard.php @@ -0,0 +1,109 @@ +exchange_rate_to_vnd = $exchange_rate_to_vnd; + + // Add VND to the Paypal supported currencies + add_filter( 'woocommerce_paypal_supported_currencies', array( $this, 'add_vnd_paypal_valid_currency' ) ); + + // Covert VND prices to the selected currency (by default, it's USD) prices before checking out with Paypal Standard + add_filter( 'woocommerce_paypal_args', array( $this, 'convert_prices' ), 11 ); + + // Add the exchange rate info for this gateway in the checkout page before proceeding in the Paypal pages + add_filter( 'option_woocommerce_paypal_settings', array( $this, 'add_exchange_rate_info' ), 11 ); + } + + /** + * @param $currencies + * + * @return mixed + */ + public function add_vnd_paypal_valid_currency( $currencies ) { + array_push( $currencies, 'VND' ); + + return $currencies; + } + + /** + * @param $paypal_args + * + * @return mixed + */ + public function convert_prices( $paypal_args ) { + if ( $paypal_args['currency_code'] == 'VND' ) { + $paypal_args['currency_code'] = $this->paypal_currency; + + $i = 1; + + while ( isset( $paypal_args[ 'amount_' . $i ] ) ) { + $paypal_args[ 'amount_' . $i ] = round( $paypal_args[ 'amount_' . $i ] / $this->exchange_rate_to_vnd, 2 ); + ++ $i; + } + if ( $paypal_args['shipping_1'] > 0 ) { + $paypal_args['shipping_1'] = round( $paypal_args['shipping_1'] / $this->exchange_rate_to_vnd, 2 ); + } + + if ( $paypal_args['discount_amount_cart'] > 0 ) { + $paypal_args['discount_amount_cart'] = round( $paypal_args['discount_amount_cart'] / $this->exchange_rate_to_vnd, 2 ); + } + if ( $paypal_args['tax_cart'] > 0 ) { + $paypal_args['tax_cart'] = round( $paypal_args['tax_cart'] / $this->exchange_rate_to_vnd, 2 ); + } + } + + return $paypal_args; + } + + + /** + * @param $value + * + * @return mixed + */ + public function add_exchange_rate_info( $value ) { + if ( ! is_admin() ) { + + $value['description'] .= '
'; + $value['description'] .= + sprintf( __( 'The prices will be converted to %1$s in the Paypal pages with the exchange rate %2$s.', 'woocommerce-for-vietnam' ), + " $this->paypal_currency", + " $this->paypal_currency / VND = $this->exchange_rate_to_vnd" + ); + + } + + return $value; + } + +} \ No newline at end of file