Skip to content

Instantly share code, notes, and snippets.

@nielsvr
Last active May 31, 2017 08:17
Show Gist options
  • Save nielsvr/4e0b4cf2f038941adf421cd1578cf26f to your computer and use it in GitHub Desktop.
Save nielsvr/4e0b4cf2f038941adf421cd1578cf26f to your computer and use it in GitHub Desktop.
Modify WooCommerce price for currency conversion and better decimal display
<?php
// define the raw_woocommerce_price callback
function modify_price_for_currency ( $price, $product ) {
global $post, $woocommerce;
if( !$product ) return $price;
$new_price = $price*0.89;
$whole = floor($new_price); // 1
$fraction = $new_price - $whole; // .25
if( $fraction > 0 && $fraction <= 0.45 ) {
$fraction = 0.45;
} elseif( $fraction > 0.45 && $fraction <= 0.95 ) {
$fraction = 0.95;
} elseif( $fraction > 0.95 && $fraction <= 0.99 ) {
$fraction = 0.99;
} else {
$fraction = 0;
}
return $whole + $fraction;
}
// add the filter
add_filter('woocommerce_get_price', 'modify_price_for_currency', $product, 10, 2 );
add_filter('woocommerce_get_regular_price', 'modify_price_for_currency', 10, 2 );
add_filter('woocommerce_get_sale_price', 'modify_price_for_currency', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment