Snippet

Round Promotional Prices to Whole Numbers in WooCommerce

How to round up prices in woocommerceWoocommerce round prices to nearest whole numberRound promotional prices to whole numbers woocommercePlugin to round prices in woocommerceRound up pricing decimals in wordpressWoocommerce price rounding settingsRound prices to nearest 10 in woocommerceRound up woocommerce prices with codeWoocommerce price rounding pluginRound 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

1<?php 2// Function to round up WooCommerce prices to the nearest whole number 3function wp_dudecom_round_woocommerce_prices( $price, $product ) { 4 // Check if the price is a valid number 5 if ( is_numeric( $price ) ) { 6 // Round the price to the nearest whole number 7 $price = ceil( $price ); 8 } 9 return $price; 10} 11 12// Hook the function to WooCommerce price filters 13add_filter( 'woocommerce_get_price', 'wp_dudecom_round_woocommerce_prices', 10, 2 ); 14add_filter( 'woocommerce_get_sale_price', 'wp_dudecom_round_woocommerce_prices', 10, 2 ); 15add_filter( 'woocommerce_get_regular_price', 'wp_dudecom_round_woocommerce_prices', 10, 2 ); 16 17// Function to round up WooCommerce cart item prices to the nearest whole number 18function wp_dudecom_round_cart_item_prices( $price, $cart_item, $cart_item_key ) { 19 // Check if the price is a valid number 20 if ( is_numeric( $price ) ) { 21 // Round the price to the nearest whole number 22 $price = ceil( $price ); 23 } 24 return $price; 25} 26 27// Hook the function to WooCommerce cart item price filter 28add_filter( 'woocommerce_cart_item_price', 'wp_dudecom_round_cart_item_prices', 10, 3 ); 29 30// Function to round up WooCommerce order item prices to the nearest whole number 31function wp_dudecom_round_order_item_prices( $price, $order_item ) { 32 // Check if the price is a valid number 33 if ( is_numeric( $price ) ) { 34 // Round the price to the nearest whole number 35 $price = ceil( $price ); 36 } 37 return $price; 38} 39 40// Hook the function to WooCommerce order item price filter 41add_filter( 'woocommerce_order_item_get_total', 'wp_dudecom_round_order_item_prices', 10, 2 ); 42?>

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:

  1. Access Your WordPress Dashboard: Log in to your WordPress admin panel.
  2. Navigate to Theme Editor: Go to Appearance > Theme Editor.
  3. Select the Theme: Choose the active theme from the dropdown menu if it's not already selected.
  4. Edit functions.php: In the right-hand sidebar, locate and click on functions.php to open it for editing.
  5. Insert the Code: Scroll to the bottom of the functions.php file and paste the provided code snippet.
  6. Save Changes: Click the Update File button to save your changes.
  7. 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.