diff --git a/inc/class-wooviet-currency.php b/inc/class-wooviet-currency.php new file mode 100644 index 0000000..c60ad3d --- /dev/null +++ b/inc/class-wooviet-currency.php @@ -0,0 +1,92 @@ + đồng" + */ + public function change_currency_symbol( $new_symbol = ' đ ' ) { + $this->new_symbol = $new_symbol; + add_filter( 'woocommerce_currency_symbol', array( $this, 'filter_woocommerce_currency_symbol' ), 10, 2 ); + } + + /** + * @see https://docs.woocommerce.com/document/change-a-currency-symbol/ + * + * @use change_currency_symbol() + * + * @param $currency_symbol + * @param $currency + * + * @return mixed + */ + public function filter_woocommerce_currency_symbol( $currency_symbol, $currency ) { + switch ( $currency ) { + case 'VND': + $currency_symbol = $this->new_symbol; + break; + } + + return $currency_symbol; + } + + /** + * Change the stupid zeros in VND to the more "human" display. + * E.g: 50000 (VND) will be 50K (VND), or 50 thousands (VND) + * + * @param string $thousand_text - Allow HTML tags like " K" + * + */ + public function convert_price_thousand_to_k( $thousand_text = ' K' ) { + $this->thousand_text = $thousand_text; + add_filter( 'formatted_woocommerce_price', array( $this, 'filter_formatted_woocommerce_price' ), 10, 5 ); + } + + /** + * @use convert_price_thousand_to_k() + * + * @param string $formatted_price + * @param float $price + * @param $decimals + * @param $decimal_separator + * @param $thousand_separator + * + * @return string + */ + public function filter_formatted_woocommerce_price( $formatted_price, $price, $decimals, $decimal_separator, $thousand_separator ) { + + if ( $price < 1000 ) { + return $formatted_price; + } else { + $new_formatted_price = number_format( $price / 1000, $decimals, $decimal_separator, $thousand_separator ) . $this->thousand_text; + + return $new_formatted_price; + } + + } +} \ No newline at end of file