Round Promotional Prices to Whole Numbers in WooCommerce
How to round up prices in woocommerce;
Woocommerce round prices to nearest whole number;
Round promotional prices to whole numbers woocommerce;
Plugin to round prices in woocommerce;
Round up pricing decimals in wordpress;
Woocommerce price rounding settings;
Round prices to nearest 10 in woocommerce;
Round up woocommerce prices with code;
Woocommerce price rounding plugin;
Round up product prices in woocommerce;
Explanation
If you're looking to make your WooCommerce prices look cleaner by rounding them up to the nearest whole number, this code snippet does just that. Here's how it works:
- Price Rounding: The code uses a function to check if the price is a number. If it is, it rounds it up to the nearest whole number using a method called ceil.
- Where It Applies: This rounding is applied to regular prices, sale prices, cart item prices, and order item prices. So, whether a customer is browsing your store, checking their cart, or reviewing their order, they'll see rounded prices.
- Integration with WooCommerce: The code hooks into WooCommerce's pricing system using filters. This means it automatically adjusts prices without needing manual updates for each product.
By using this code, you ensure that all displayed prices are neat and rounded, which can enhance the shopping experience by making prices easier to read and understand.
Code
<?php
// Function to round up WooCommerce prices to the nearest whole number
function wp_dudecom_round_woocommerce_prices( $price, $product ) {
// Check if the price is a valid number
if ( is_numeric( $price ) ) {
// Round the price to the nearest whole number
$price = ceil( $price );
}
return $price;
}
// Hook the function to WooCommerce price filters
add_filter( 'woocommerce_get_price', 'wp_dudecom_round_woocommerce_prices', 10, 2 );
add_filter( 'woocommerce_get_sale_price', 'wp_dudecom_round_woocommerce_prices', 10, 2 );
add_filter( 'woocommerce_get_regular_price', 'wp_dudecom_round_woocommerce_prices', 10, 2 );
// Function to round up WooCommerce cart item prices to the nearest whole number
function wp_dudecom_round_cart_item_prices( $price, $cart_item, $cart_item_key ) {
// Check if the price is a valid number
if ( is_numeric( $price ) ) {
// Round the price to the nearest whole number
$price = ceil( $price );
}
return $price;
}
// Hook the function to WooCommerce cart item price filter
add_filter( 'woocommerce_cart_item_price', 'wp_dudecom_round_cart_item_prices', 10, 3 );
// Function to round up WooCommerce order item prices to the nearest whole number
function wp_dudecom_round_order_item_prices( $price, $order_item ) {
// Check if the price is a valid number
if ( is_numeric( $price ) ) {
// Round the price to the nearest whole number
$price = ceil( $price );
}
return $price;
}
// Hook the function to WooCommerce order item price filter
add_filter( 'woocommerce_order_item_get_total', 'wp_dudecom_round_order_item_prices', 10, 2 );
?>
Instructions
To implement the code for rounding WooCommerce prices to whole numbers, follow these steps:
File Location: Add the code to your theme's functions.php
file or create a custom plugin file.
Prerequisites:
- Ensure WooCommerce is installed and activated on your WordPress site.
Implementation Steps:
- Access Your WordPress Dashboard: Log in to your WordPress admin panel.
- Navigate to Theme Editor: Go to Appearance > Theme Editor.
- Select the Theme: Choose the active theme from the dropdown menu if it's not already selected.
- Edit functions.php: In the right-hand sidebar, locate and click on functions.php to open it for editing.
- Insert the Code: Scroll to the bottom of the
functions.php
file and paste the provided code snippet. - Save Changes: Click the Update File button to save your changes.
- Test the Changes: Visit your WooCommerce store and check product, cart, and order pages to ensure prices are rounded as expected.
If you encounter any issues or need further assistance with implementation or advanced functionality, consider reaching out to wp-dude.com for expert help.