Skip to content

Instantly share code, notes, and snippets.

@kanasite
Last active April 11, 2016 14:47
Show Gist options
  • Save kanasite/44f5c52a8dda1c61cb99d073055a4935 to your computer and use it in GitHub Desktop.
Save kanasite/44f5c52a8dda1c61cb99d073055a4935 to your computer and use it in GitHub Desktop.
Rounding
<?php
/*
* Plugin Name: Woocommerce Rounding
* Description: Round woocommerce orders total
* Plugin URI: http://www.amintaibouta.com
* Author: Amin T.
* Author URI: http://www.amintaibouta.com
* Version: 1.0
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
add_action( 'woocommerce_cart_calculate_fees', 'wrpp_add_cart_fee' );
function wrpp_add_cart_fee() {
global $woocommerce;
//Get the cart total and round it
$total = number_format($woocommerce->cart->subtotal,2);
$round = wrpp_round_price($total);
//Add fee item +/- to the cart
$woocommerce->cart->add_fee( __('Rounding Adjustment', 'woocommerce'), $round - $total ,true,'');
}
//Get the rounded value based on the last float value
function wrpp_round_price($total){
//Get the floating part of the total
$total = str_replace(',', '', $total);
$float_part = number_format(round($total - intval($total), 2), 2, ".", "");
//Get the last nunmber of the floating part
$index = substr($float_part,-1);
switch ($index)
{
case "1":
return $total - 0.01;
break;
case "2":
return $total - 0.02;
break;
case "3":
return $total + 0.02;
break;
case "4":
return $total + 0.01;
break;
case "6":
return $total - 0.01;
break;
case "7":
return $total - 0.02;
break;
case "8":
return $total + 0.02;
break;
case "9":
return $total + 0.01;
break;
default:
return $total;
break;
}
}
//Set the new item in the right place
function wrpp_load_scripts() {
wp_enqueue_script('custom-js', WP_PLUGIN_URL . '/woocommerce-rounding/woocommerce-rounding.js',array('jquery'));
}
add_action('wp_enqueue_scripts', 'wrpp_load_scripts');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment