John Davidson

php - WooCommerce - Price was converted twice on cart/checkout page

0 comments
Message:


The currency conversion plugin I use for my quote request website works great until I redirect from an email to the Cart page. I am using GBP as the base currency and USD, EUR, CAD, AED as the other currencies.


If I make a request in USD (or any other), the price in the email gets converted a second time when I am redirected to the cart page, which distorts the value a customer receives. I believe it assumes the price coming from the email is GBP and thus does a second conversion, which is not needed.


I found a supposed fix for this but it was for a single currency.


//We are reversing the price of product to USD in cart page, because the rate is applied again there
add_action('woocommerce_before_calculate_totals', 'fix_currency_conversion');
function fix_currency_conversion($cart_object){
global $WOOCS;
$cur_currency = $WOOCS->current_currency;
$currencies = $WOOCS->get_currencies();
$conversion_rate = $currencies[$WOOCS->current_currency]['rate'];
if($cur_currency == 'HKD'){
foreach ( $cart_object->cart_contents as $cart_item_key => $cart_item ) {
$cart_item['data']->price = $cart_item['data']->price / $conversion_rate;
}
}
}

Can you please suggest how I might be able to add more currencies? I already tried this but my php is not so strong. I added this line in the place of the if statement above:


// Do not convert these currencies
$keep_rate = array( 'NGN', 'USD', 'CAD', 'EUR', 'AED' );

// Don’t convert currencies when it's one of these rates
if ( has_term( $keep_rate, 'currencies', $WOOCS->get_currencies() ) ) {
foreach ( $cart_object->cart_contents as $cart_item_key => $cart_item ) {
$cart_item['data']->price = $cart_item['data']->price / $conversion_rate;
}
}


My Answer:
>current_currency;_ foreach ($cart_object->get_cart() as $cart_item_key => $cart_item){_ $product_id = $cart_item['product_id'];_ $product = wc_get_product($product_id);_ $price = $product->get_price();_ if($cur_currency == 'USD'){_ $price = $price / $WOOCS->get_currencies_rates('USD');_ $cart_item['data']->set_price($price);_ }_ }_ }_

This code snippet should help reverse the price conversion for USD in the cart page. You can modify it to include other currencies as well by adding additional conditions for each currency. Make sure to test it thoroughly before implementing it on your live website.

Rate this post

4 of 5 based on 1744 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us