I am struggling to find a solution to my price format issue.
If the price in cart adds up to $1 then it should show $1.00 Now, this works if I change the decimal in WC settings to "2".
However, if wanted it to show as 0.065 and 0.010 So I added a "3" in the WC decimal settings. So, how can I get the price to look like this:
$0.065
$0.01 (and NOT this 0.010)
I just want to remove the trailing zero if it is a zero.
So I have tried a few things:
add_filter( 'woocommerce_price_trim_zeros', 'wc_hide_trailing_zeros', 10, 1 );
function wc_hide_trailing_zeros( $trim ) {
return true;
}
This removes ALL zeros.
Then I tried a function that supposed to remove the last item in a str.
function rstrtrim($str, $remove=null)
{
$str = (string)$str;
$remove = (string)$remove;
if(empty($remove))
{
return rtrim($str);
}
$len = strlen($remove);
$offset = strlen($str)-$len;
while($offset > 0 && $offset == strpos($str, $remove, $offset))
{
$str = substr($str, 0, $offset);
$offset = strlen($str)-$len;
}
return rtrim($str);
}
//Remove last zero on a 3 decimal setting
echo rstrtrim((string)$number_format, '0');
But I think it's not working because it is reading it as a string and not an integer, dunno.
So, all I want to do is set my decimal settings in WC to "3" and remove the "last" integer IF it is a zero...
so this:
$1.000
$0.065
$0.010
Would be this:
$1.00
$0.065
$0.01
Ok, I figured it out!
add_filter( 'formatted_woocommerce_price', 'wc_custom_price_format', 10, 5 );
function wc_custom_price_format( $number_format, $price, $decimals, $decimal_separator, $thousand_separator){
$lastnum = $number_format[strlen($number_format)-1];
if ($lastnum == 0):
return substr($number_format, 0, -1);
else:
return $number_format;
endif;
}
Related
I am trying to add a random string when the order number is created as the default sequential number can be very easily guessed.
I tried this snippet:
function generate_random_string( $length = 16 ) {
return substr( str_shuffle( str_repeat( $x = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil( $length / strlen( $x ) ) ) ), 1, $length );
}
add_filter( 'woocommerce_order_number', 'ct_change_woocommerce_order_number', 1, 2);
function ct_change_woocommerce_order_number( $order_id, $order ) {
$random_string1 = generate_random_string(5);
return $random_string1 . $order->get_id();
}
The problem is that this change the order number every time the order number is requested somewhere.
This would work if I will use a constant prefix and suffix, but in the actual way a different order number is shown each time for the same order. Any advice?
To prevent this you can save the result as meta data, once this exists return the meta data instead of the result of the function
So you get:
function generate_random_string( $length = 16 ) {
return substr( str_shuffle( str_repeat( $x = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil( $length / strlen( $x ) ) ) ), 1, $length );
}
function filter_woocommerce_order_number( $order_number, $order ) {
// Get meta
$random_meta_string = $order->get_meta( '_random_meta_string' );
// When meta empty
if ( empty ( $random_meta_string ) ) {
// Call function
$generate_random_string = generate_random_string( 5 );
// Append
$random_string = $generate_random_string . $order->get_id();
// Add the meta data
$order->update_meta_data( '_random_meta_string', $random_string );
// Return random string
$order_number = $random_string;
} else {
// Return meta
$order_number = $random_meta_string;
}
return $order_number;
}
add_filter( 'woocommerce_order_number', 'filter_woocommerce_order_number', 10, 2 );
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 );
I have a product with variations, that is being displayed by the templates/single-product/add-to-cart/variation.php template, which uses JavaScript-based templates {{{ data.variation.display_price }}}. When I have price that end with a zero, for example, € 12.50, the price on the front-end will be displayed as € 12.5 (without the zero). I want to have the price include the trailing zero.
I've tried the following filter, but it does not work.
add_filter( 'woocommerce_price_trim_zeros', 'wc_hide_trailing_zeros', 10, 1 );
function wc_hide_trailing_zeros( $trim ) {
// set to false to show trailing zeros
return false;
}
I've fixed it by checking that when the price has one decimal, add a zero.
// https://stackoverflow.com/a/2430214/3689325
function numberOfDecimals( $value ) {
if ( (int) $value == $value ) {
return 0;
}
else if ( ! is_numeric( $value ) ) {
return false;
}
return strlen( $value ) - strrpos( $value, '.' ) - 1;
}
/**
* Make sure prices have two decimals.
*/
add_filter( 'woocommerce_get_price_including_tax', 'price_two_decimals', 10, 1 );
add_filter( 'woocommerce_get_price_excluding_tax', 'price_two_decimals', 10, 1 );
function price_two_decimals( $price ) {
if ( numberOfDecimals( $price ) === 1 ) {
$price = number_format( $price, 2 );
return $price;
}
return $price;
}
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.
I expected: Default Values MyValue NEW22! Default Values MyValue NEW!
But am getting: Default Values MyValue NEW22! NEW!
Can anyone tell me why?
Code:
// First call this
function example_callback2( $string2 ) {
$new_value2 = $string2 . " NEW22!";
return $new_value2;
}
add_filter( 'example_filtersubroto', 'example_callback2', 9 );
$value = "Default Values MyValue";
echo $value = apply_filters( 'example_filtersubroto', $value );
// Expected output: Default Values MyValue NEW22! Default Values MyValue NEW!
// Real output: Default Values MyValue NEW22! NEW!
?>
Result:
Do want like this?
// First call this
function example_callback2( $string2 ) {
$new_value2 = $string2 . " NEW22!";
$new_value2 .= $new_value2;
return $new_value2;
}
add_filter( 'example_filtersubroto', 'example_callback2', 9 );
$value = "Default Values MyValue";
echo $value = apply_filters( 'example_filtersubroto', $value );