【问题标题】:Get Woocommerce currency symbol from order in YITH invoice plugin从 YITH 发票插件中的订单中获取 Woocommerce 货币符号
【发布时间】:2017-10-06 13:31:17
【问题描述】:

我们的 WooCommerce 网站有 2 种货币。主要货币是印度 卢比,次要货币是 USD(使用货币切换器)。我尝试用美元下订单,但 YITH 发票显示发票中的主要印度卢比符号。

我已尝试更改为各种可用的货币切换器插件,但发票中的符号不​​会更改,它只是采用默认货币符号。

我什至尝试将“get_woocommerce_currency_symbol()”添加到 YITH 函数的货币 arg 数组中。我需要帮助。使用的插件是 YITH Invoice 版本:1.3.11。

function yith_get_formatted_price ( $price, $args = array () ) {
    extract ( apply_filters ( 'wc_price_args', wp_parse_args ( $args, array (
        'ex_tax_label'       => false,
        'currency'           => get_woocommerce_currency_symbol (),
        'decimal_separator'  => wc_get_price_decimal_separator (),
        'thousand_separator' => wc_get_price_thousand_separator (),
        'decimals'           => wc_get_price_decimals (),
        'price_format'       => get_woocommerce_price_format (),
    ) ) ) );

    $negative = $price < 0;
    $price    = apply_filters ( 'raw_woocommerce_price', floatval ( $negative ? $price * - 1 : $price ) );
    $price    = apply_filters ( 'formatted_woocommerce_price', number_format ( $price, $decimals, $decimal_separator, $thousand_separator ), $price, $decimals, $decimal_separator, $thousand_separator );

    if ( apply_filters ( 'woocommerce_price_trim_zeros', false ) && $decimals > 0 ) {
        $price = wc_trim_zeros ( $price );
    }

    $formatted_price = ( $negative ? '-' : '' ) . sprintf ( $price_format, get_woocommerce_currency_symbol ( $currency ), $price );
    $return          = $formatted_price;

    return apply_filters ( 'wc_price', $return, $price, $args );
}

【问题讨论】:

    标签: php wordpress woocommerce currency orders


    【解决方案1】:

    获取订单货币符号(或代码)的唯一方法是首先获取WC_Order对象,您可以从global $order;对象或来自$order_id 与:

    $order = wc_get_order( $order_id ); 
    

    现在你可以在上面使用WC_Abstract_Order方法get_currency()来获取货币代码,最后你会这样得到货币符号:

    $currency_code = $order->get_currency();
    $currency_symbol = get_woocommerce_currency_symbol( $currency_code );
    

    这是在 WooCommerce 3+ 上测试和工作的

    【讨论】:

    • 是的,效果很好。由于 $current_order = $document->order;已在发票功能页面中定义。然后我不得不添加一个方法 //global $woocommerce; $order_currency = $current_order->get_order_currency();后跟 array( 'currency' => $order_currency) ,其中 wc_price 用于获取价格。
    猜你喜欢
    • 2014-12-07
    • 2017-06-13
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    相关资源
    最近更新 更多