3
votes

In our store we enter prices including tax. On frontend we hide decimals (In Sweden we don't really use decimals anymore in prices), but backend we keep them. This is why: https://krokedil.com/dont-display-prices-with-0-decimals-in-woocommerce/

We use the function from the link above to hide the decimals on frontend and have also added a function to round off coupons at front end.

/**
 * Trim zeros in price decimals
 **/
 add_filter( 'woocommerce_price_trim_zeros', '__return_true' );
function iconic_was_loop_position( $position ) {
    return 'woocommerce_after_shop_loop_item_title';
}

add_filter( 'iconic_was_loop_position', 'iconic_was_loop_position', 10, 1 );

/**
* 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 );

This works well for both prices and percentage coupons/discounts. However, say I have a coupon with a fixed cart amount of SEK400, when using it the amount instead changes to SEK402. I guess this has to do with how woo applies the coupons combined with the rounding filter.

So my question is, is it possible to exclude the fixed cart coupons from the rounding function in some way?

1

1 Answers

1
votes

This can be done very easily using the WC_Coupon conditional method is_type(). In your hooked function filter_woocommerce_coupon_get_discount_amount() you will use last argument $instance that is the WC_Coupon Object in an if statement, this way:

add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
function filter_woocommerce_coupon_get_discount_amount( $discount, $discounting_amount, $cart_item, $single, $instance ) {
    // Round the discount for all other coupon types than 'fixed_cart'
    if( ! $instance->is_type('fixed_cart') )
        $discount = ceil( $discount );

    return $discount; 
}   

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This should work…