Price calculations displaying 2 Currencies in WooCommerce Checkout Subtotal &Total line - woocommerce

ETH 5 ($ 3.753,65)
I want to show two currencies in checkout Subtotal & Totals in woocommerce. I'm guessing i'm using the wrong hook?
Example Image
// Convert to USD
function convert_price_cart( $price ){
$convertion_rate = 780.730022;
$new_price = $price * $convertion_rate;
return number_format($new_price, 2, '.', '');
}
add_filter( 'wc_price', 'my_custom_price_format', 10, 3 );
function my_custom_price_format( $formatted_price, $price, $args ) {
// The currency conversion custom calculation function
$price_usd = convert_price_cart($price);
// the currency symbol for US dollars
$currency = 'USD';
$currency_symbol = get_woocommerce_currency_symbol( $currency );
$price_usd = $currency_symbol.$price_usd; // adding currency symbol
// The USD formatted price
$formatted_price_usd = "<span class='price-usd'> ($price_usd)</span>";
// Return both formatted currencies
return $formatted_price . $formatted_price_usd;
}
Is there anybody could help me with this situation.

Related

Display product cost and total price (cart + product cost) on the product page

I have the following code:
`
add_action( 'woocommerce_after_add_to_cart_form', 'total_product_and_subotal_price' );
function total_product_and_subotal_price() {
global $product;
$product_price = (float) wc_get_price_to_display( $product );
$cart_subtotal = (float) WC()->cart->subtotal + $product_price;
$price_0_html = wc_price( 0 ); // WooCommmerce formatted zero price (formatted model)
$price_html = '<span class="amount">'.number_format($product_price, 2, ',', ' ').'</span>';
$subtotal_html = '<span class="amount">'.number_format($cart_subtotal, 2, ',', ' ').'</span>';
// Display formatted product price total and cart subtotal amounts
printf('<div id="totals-section"><p class="product-total">%s</p><p class="cart-subtotal">%s</p></div>',
str_replace([' amount','0,00'], ['',$price_html], $price_0_html), // formatted html product price
str_replace([' amount','0,00'], ['',$subtotal_html], $price_0_html) // Formatted html cart subtotal
);
?>
<script>
jQuery( function($){
var productPrice = <?php echo $product_price; ?>,
startCartSubtotal = <?php echo $cart_subtotal; ?>;
function formatNumber( floatNumber ) {
return floatNumber.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ').replace('.', ',');
}
$('input[name=quantity]').on( 'input change', function(){
var productQty = $(this).val() == '' ? 1 : $(this).val(),
productTotal = parseFloat(productPrice * productQty),
cartSubtotal = productTotal + startCartSubtotal - productPrice;
cartSubtotal = $(this).val() > 1 ? parseFloat(cartSubtotal) : parseFloat(startCartSubtotal);
$('#totals-section > .product-total .amount').html( formatNumber(productTotal) );
$('#totals-section > .cart-subtotal .amount').html( formatNumber(cartSubtotal) );
});
});
</script>
<?php
}
`
This code shows me the total value of the product and the total value of the basket + the total value of the product before the product is added to the basket (they are displayed on the product page). For simple products it works correctly, for variable products it calculates me at the lowest price regardless of the price of the selected variation. How could I make the selected variation be taken into account, not the cheapest one?
I added it to functions.php, but, as I wrote in the description, it displays the minimum price, not the price of the selected variation.
Thank you very much!

Show max variable price in shop page - Woocommerce

I have this code to display the price.
function edit_selected_variation_price( $data, $product, $variation ) {
$price = $variation->price;
$price_incl_tax = $price + round($price * ( 23 / 100 ), 2);
$price_incl_tax = number_format($price_incl_tax, 2, ",", ".");
$price = number_format($price, 2, ",", ".");
$display_price = '<div><span class="price">';
$display_price .= '<span>Cena netto</span><span class="amount">' . $price .'<small class="woocommerce-price-suffix"> zł</small></span>';
$display_price .= '</div><div>';
$display_price .= '<span>Cena brutto</span><span class="amount">' . $price_incl_tax .'<small class="woocommerce-price-suffix"> zł</small></span>';
$display_price .= '</span></div>';
$data['price_html'] = $display_price;
return $data;
}
add_filter( 'woocommerce_available_variation', 'edit_selected_variation_price', 10, 3);
How to get the maximum price displayed without choosing any variants?
I do not fully understand your question, you want the max price from what prices (incl tax and ...)?
However, when you have different prices in an array (or as loose variables), consider using the PHP max() function:
https://www.php.net/manual/en/function.max.php
That will return the highest value (Hope that's what you're after)

Round cart total and subtotal decimals off to nearest 5 unit in WooCommerce

I have been working on the cart value roundup to the nearest value and created a function but it roundups the complete value.
What am I doing wrong?
//round cart total up to nearest dollar
add_filter( 'woocommerce_calculated_total', 'custom_calculated_total' );
function custom_calculated_total( $total ) {
$total = round( $total );
return ceil($total / 5) * 5;
}
If I have the values 44.24 I want to output as 44.25 and if I have the value as 44.28 then the output should be as 44.30
First of all you will need to use the woocommerce_cart_subtotal filter hook in addition to the woocommerce_calculated_total filter hook.
Otherwise your subtotal will deviate from the total, which seems weird
Then you can use 1 of the answers from: Rounding Mechanism to nearest 0.05
So you get:
function rnd_func( $x ) {
return round( $x * 2, 1 ) / 2;
}
function filter_woocommerce_cart_subtotal( $subtotal, $compound, $cart ) {
// Get cart subtotal
$round = rnd_func( $cart->subtotal );
// Use wc_price(), for the correct HTML output
$subtotal = wc_price( $round );
return $subtotal;
}
add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );
// Allow plugins to filter the grand total, and sum the cart totals in case of modifications.
function filter_woocommerce_calculated_total( $total, $cart ) {
return rnd_func( $total );
}
add_filter( 'woocommerce_calculated_total', 'filter_woocommerce_calculated_total', 10, 2 );

How to adjust prices according to weight on woocommerce?

I'd like to use Woocommerce over WordPress for my shop, but clients should be able to buy the product by either unit or weight.
If X units were bought, we must measure the actual weight of the purchase (obtained during the picking and packing of the order), and adjust the price accordingly.
Is there an easy way to do this on Woocommerce?
Thanks
You can do your modification using follows code snippet -
function adjust_price_based_on_weight( $cart_object ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = $cart_item['data'];
$product_id = $cart_item['product_id'];
$quantity = $cart_item['quantity'];
$product_price = floatval( $_product->get_price() ); // Actual product price
// Do your calculation based on product quantity and weight
// for example suppose product per unit weight 2 kgs and add to cart number of unit is 3.
$product_unit_weights = $quantity * 2; // where 2kg/per unit and $quantity is 3
if( $product_unit_weights > 5 ) { // if units weights is greter than 5 kgs additional cost 10.
$additionalPrice = 10;
$_product->set_price( $product_price + $additionalPrice );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'adjust_price_based_on_weight', 99 );

convert price on woocommerce

My products price is based on USD , but i want change USD to IRR with a Conversion Rate (1 USD = 3500 IRR) in shop and checkout . I found this code but it works only for product veiw and when i click on cart or checkout price change to USD .
add_filter( 'woocommerce_get_price_html', 'wpa83367_price_html', 100, 2 );
function wpa83367_price_html( $price, $product ){
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$toman = 3500;
$total = $price * $toman . ' ' . 'تومان';
return $total;
}

Resources