函数结果的乘法会导致计算错误(WordPress)

huangapple go评论55阅读模式
英文:

Multiplication of function result breaks calculation (WordPress)

问题

这段代码中存在问题,当货币为THB(泰铢)时,代码将转换后的总额乘以2,但这会导致错误的结果。您可以尝试以下更改:

将这行代码:

$converted_total_gptlove *= 2;

改为:

if ( $currency_to === 'thb' ) {
    $converted_total_gptlove *= 2;
}

这将确保只有当货币是THB时才执行乘法操作,从而修复了问题。

英文:

I have some code that uses a plugin that does currency conversion. Depending on the payment method, the code creates a converted order total and later saves it as a custom field. I would like to perform a multiplication if the conversion is to a specific currency, but it breaks the conversion functionality and returns a "4" instead of a converted total. Any idea what is wrong?

This code works and converts the order total correctly:

// Add custom order meta data on checkout
add_action( 'woocommerce_checkout_create_order', 'add_custom_order_meta', 10, 2 );

function add_custom_order_meta( $order, $data ) {
    // Get the payment method for the order
    $payment_method = $order->get_payment_method();

    // Only add custom meta data for certain payment methods
    if ( in_array( $payment_method, array( 'bacs', 'cheque' ) ) ) {
        // Get the order total
        $order_total = $order->get_total();
        
        // Set the currency to convert to based on payment method
        $currency_to = ( $payment_method === 'bacs' ) ? 'hkd' : 'thb';
        
        // Convert the order total to the selected currency
        $converted_total_gptlove = get_conversion( 'number=' . $order_total . '&from=usd&to=' . $currency_to . '&dp=2' );
        
        // Set the currency symbol and code based on payment method
        $currency_symbol = ( $payment_method === 'bacs' ) ? '$' : '฿';
        $currency_code = ( $payment_method === 'bacs' ) ? 'HKD' : 'THB';
        
        // Update the order meta data with the converted total and currency info
        $order->update_meta_data( 'converted_total_gptlove', $currency_symbol . $converted_total_gptlove . ' ' . $currency_code );
    }
}

This code adds the multiplication, and returns a 4 instead of the converted total multiplied by 2:

// Add custom order meta data on checkout
add_action( 'woocommerce_checkout_create_order', 'add_custom_order_meta', 10, 2 );

function add_custom_order_meta( $order, $data ) {
    // Get the payment method for the order
    $payment_method = $order->get_payment_method();

    // Only add custom meta data for certain payment methods
    if ( in_array( $payment_method, array( 'bacs', 'cheque' ) ) ) {
        // Get the order total
        $order_total = $order->get_total();
        
        // Set the currency to convert to based on payment method
        $currency_to = ( $payment_method === 'bacs' ) ? 'hkd' : 'thb';
        
        // Convert the order total to the selected currency
        $converted_total_gptlove = get_conversion( 'number=' . $order_total . '&from=usd&to=' . $currency_to . '&dp=2' );
        
        // Multiply the converted total by 2 if currency is THB
        if ( $currency_to === 'thb' ) {
            $converted_total_gptlove *= 2;
        }
        
        // Set the currency symbol and code based on payment method
        $currency_symbol = ( $payment_method === 'bacs' ) ? '$' : '฿';
        $currency_code = ( $payment_method === 'bacs' ) ? 'HKD' : 'THB';
        
        // Update the order meta data with the converted total and currency info
        $order->update_meta_data( 'converted_total_gptlove', $currency_symbol . $converted_total_gptlove . ' ' . $currency_code );
    }
}

Could someone help pointing me in the right direction?

答案1

得分: 0

问题已解决,通过在转换函数之前对订单总额进行计算。

// 如果货币是THB,则将订单总额乘以2
$converted_order_total = $order_total;
if ($currency_to === 'thb') {
$converted_order_total *= 1.027;
}

英文:

The problem has been solved, by performing the calculation on the order total, before converting it with the function.

    // Multiply the order total by 2 if currency to is THB
    $converted_order_total = $order_total;
    if ($currency_to === 'thb') {
        $converted_order_total *= 1.027;
    }

huangapple
  • 本文由 发表于 2023年4月17日 22:01:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定