In this post, we are going to look into how Measurements Price Calculator extension works and how to modify certain values that it produces as a result.

If at any time you’ll have questions about the product setup, check out extension’s documentation here as we are going to cover only specific product configuration but there is much more to this extension than what will be in this post.

Let’s assume you are selling a Crushed Stone at $34.00 / cubic yard. You have a user-defined mode enabled and you offer your users to enter the dimensions of the area they need to fill in with the stone so that the total number of cubic yards they need to buy could be calculated for them:

Your user is able to enter the Length, Width, and Height and the extension calculated the Total Volume that they need to purchase. Awesome! Then the user adds the product to the cart and sees the following:

I think everyone agrees that this is not very pleasant for the user’s experience so you want to do something about it and remove part or all of the decimals after the dot. Let’s see how we can do it.

First, let’s look at how our product is set in WP Admin:

General Section:

Measurement Section:

Nothing is set up in other sections so you may skip them.

Now, how do we get rid of those decimals? By using a code snippet. Measurement Price Calculator extension developers SkyVerge provided several snippets for the plugin and its collection could be found here. We are going to use the one below:

<?php // only copy this line if needed
/**
 * Change the Measurement Price Calculator measurement precision to 2 decimal
 * places
 *
 * This controls the number of decimal places used to display the total
 * measurement on product pages
 *
 * @return int The number of decimal places
 */
function sv_wc_measurement_price_calculator_change_measurement_precision() {
	return 2;
}
add_filter( 'wc_measurement_price_calculator_measurement_precision', 'sv_wc_measurement_price_calculator_change_measurement_precision' );
/**
 * Round the Measurement Price Calculator total measurement on the cart page to
 * 2 decimal places.
 *
 * Requrires v3.10.2+
 *
 * @param float $value The total measurement value
 * @return float The total measurement value rounded to 2 decimal places
 */
function sv_wc_measurement_price_calculator_humanize_cart_item_data_total_amount_value( $value ) {
	return round( $value, 2 );
}
add_filter( 'wc_measurement_price_calculator_cart_item_data_total_amount_value', 'sv_wc_measurement_price_calculator_humanize_cart_item_data_total_amount_value' );

To implement the snippet, we are going to use a plugin called Code Snippets. You can read the post I wrote about where to download it and how to use it here.

The snippet above will return the Total value with 2 decimals after the dot. The line of code responsible for the change is this one:

return round( $value, 2 );

In order to get rid of all the decimals, change the value from 2 to 0 in this line. The final result should look as follows:

return round( $value, 0 );

Now, let’s insert our snippet into the code snippet input area:

Save and activate the changes.

Now let’s look at our final result:

We are all set! Our Total Value had been rounded to the nearest full cubic yard and we have no decimals after the dot.

Feel free to ask follow-up questions in comments.

Happy Rounding! 🙂

Posted by Julia Amosova

I love traveling, New York City, coffee, my husband, late mornings, taking pictures, new cool technologies and good books.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s