woocommerce coupon round to 1 decimal - woocommerce

In my store I'm using this code to round off decimals in coupons.
Is it posible to round numbers from 99.555 to 99.9?
I need only one decimal.
/**
* Round off decimals for coupons
**/
function filter_woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $instance ) {
$discount = ceil( $discount );
return $discount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );

You can use PHP round function (instead of ceil) which has a $precision parameter which you can set to 1. This will round up to the closets precision which for 9.555 is 9.6:
/**
* Round off decimals for coupons
**/
function filter_woocommerce_coupon_get_discount_amount( $discount,
$discounting_amount, $cart_item, $single, $instance ) {
$discount = round( $discount, 1 );
return $discount;
}
add_filter( 'woocommerce_coupon_get_discount_amount',
'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
If you want to convert 9.555 to 9.9, then you'll have to create that logic, something like this for example:
/**
* Round off decimals for coupons
**/
function filter_woocommerce_coupon_get_discount_amount( $discount,
$discounting_amount, $cart_item, $single, $instance ) {
$disvount_floor = floor($discount);
// This will return x.99 if $discount decimal number is > 0.5
if (fmod($discount, $disvount_floor) > 0.5) {
return "{$disvount_floor}.9";
}
return $discount;
}
add_filter( 'woocommerce_coupon_get_discount_amount',
'filter_woocommerce_coupon_get_discount_amount', 10, 5 );

Related

Sup in Woocommerce with comma for decimal

I wrote the following code to change the position and appearance of the decimal places.
add_filter( 'formatted_woocommerce_price', 'ts_woo_decimal_price', 10, 5 );
function ts_woo_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
$unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
$decimal = sprintf( '%02d', ( $price - intval( $price ) ) * 100 );
return $unit . '<sup>' . $decimal . '</sup>';
}
The code works so far that the decimal places are superscript, but now I'm missing the comma.

Apply Coupon code on the final total amount, not cart total

Apply Coupon code on the final total amount, not cart total:
/*Installtion Chrages Discount */
add_action( 'woocommerce_cart_calculate_fees', 'mysite_box_discount', 10, 1);
function mysite_box_discount( $cart )
{
$installation_price = 100;
$cart->add_fee( __($cartQty.'Installation', 'woocommerce'),$installation_price,false );
global $woocommerce;
if(WC()->cart->get_applied_coupons()){
$coupon_code = WC()->cart->get_applied_coupons();
$coupon = new WC_Coupon($coupon_code[0]);
$coupon_amount = (float) $coupon->amount;
$discount_on_fee = (float) $coupon_amount/100;
$installation_discount = $discount_on_fee*$installation_price;
$cart->add_fee( __($cartQty.'Installation Discount', 'woocommerce'), -$installation_fee_discount,false );
}
}
This apply discount but I need complete total discount including fees too.

"WC()->cart->get_subtotal()" returns 0 when using "woocommerce_product_get_tax_class" hook

I'm trying to drop the VAT on my cart items when the subtotal is over a specific amount. But when I use WC()->cart->get_subtotal() to get the subtotal price, it returns 0 and my if statement fails.
add_action( 'woocommerce_product_get_tax_class', 'wp_check_uk_vat', 1, 2 );
function wp_check_uk_vat( $tax_class, $product ) {
$cart_total = WC()->cart->get_subtotal();
// echo $cart_total;
if( $cart_total >= 100 ) {
$tax_class = "Zero rate";
}
return $tax_class;
}
Similar code is used here: https://docs.woocommerce.com/document/setting-up-taxes-in-woocommerce/#section-18
The goal of the code is to drop the VAT on the cart (not shipping) when the order amount is over 135 GBP (to comply with the new Brexit rules for merchants).
Because WC()->cart->get_subtotal() returns 0, the if statement fails, and the VAT is not being dropped.
Try this
add_action( 'woocommerce_product_get_tax_class', 'wp_check_uk_vat', 1, 2 );
function wp_check_uk_vat( $tax_class, $product ) {
if(WC()->cart){
$subtotal = 0;
foreach ( WC()->cart->get_cart() as $cart_item ) {
$subtotal += $cart_item[ 'data' ]->get_price( 'edit' ) * $cart_item[ 'quantity' ];
}
if ( $subtotal >= 100 ) {
$tax_class = "Zero rate";
}
}
return $tax_class;
}

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 get WooCommerce variation prices to have two decimals (trailing zero)?

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;
}

Resources