如何向Woocommerce货币过滤器添加多种货币

时间:2015-01-24 16:15:19

标签: php arrays woocommerce wordpress

我知道PHP很少,我确信这很简单,但我卡住了:-(。 Woocommerce有一个货币过滤器,可用于添加更多货币。这就是他们在their documentation中提供的代码的外观:

add_filter( 'woocommerce_currencies', 'add_my_currency' );

function add_my_currency( $currencies ) {
 $currencies['ABC'] = __( 'Currency name', 'woocommerce' );
 return $currencies;
}

add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2);

function add_my_currency_symbol( $currency_symbol, $currency ) {
 switch( $currency ) {
      case 'ABC': $currency_symbol = '$'; break;
 }
 return $currency_symbol;
}

如何添加多种货币? (在我看来它应该是这样的)

add_filter( 'woocommerce_currencies', 'add_my_currency' );

function add_my_currency( $currencies ) {
 $currencies['ABC'] = __( 'Currency name', 'woocommerce' );
 $currencies['DEF'] = __( 'Currency name 2', 'woocommerce' );
 return $currencies;
}

add_filter('woocommerce_currency_symbol', 'add_my_currency_symbol', 10, 2);

function add_my_currency_symbol( $currency_symbol, $currency ) {
 switch( $currency ) {
      case 'ABC': $currency_symbol = 'ABC'; break;
      case 'DEF': $currency_symbol = 'DEF'; break;
 }
 return $currency_symbol;

1 个答案:

答案 0 :(得分:0)

想出来了。我需要做的就是更改过滤器的第二个参数和相应的函数名称,如下所示:

// Woocommerce add Malawi Kwacha (MK)

add_filter( 'woocommerce_currencies', 'add_mk' );  
function add_mk( $currencies ) {
  if(!isset($currencies['MK'])||!isset($currencies['MK'])){
 $currencies['MK'] = __( 'Malawi Kwacha', 'woocommerce' );
 return $currencies;
 }
}
add_filter('woocommerce_currency_symbol', 'add_mk_symbol', 10, 2);
function add_mk_symbol( $currency_symbol, $currency ) {
switch( $currency ) {
  case 'MK': $currency_symbol = 'K'; break;
}
return $currency_symbol;
}