WooCommerce: Set Product Discount Percentage @ Product Admin

Let’s say you want to apply a 10% discount on a WooCommerce product. Its original price is $79.56. You go to the “Edit Product” page, go to the “Sale Price” input field, and enter ( $79.56 – 10% ) = $71.63. This is great as you can set the sale price, but this forces you to do some math and waste time.

What if there were a custom select dropdown, where you could directly define a fixed discount e.g. 10% or 25%, without having to calculate the final price?

Well, in today’s tutorial, we’ll see how we can display a dropdown in the Product Edit page, and at the same time how to edit the frontend price once a discount value is selected, so that you don’t need to worry about that manual sale price calculation. Enjoy!

With this custom select dropdown, I can define the WooCommerce product sale price by selecting a fixed discount rate, and stop worrying about math!

PHP Snippet: Define Product Discount Via a Custom Dropdown @ WooCommerce Edit Product Page

/**
 * @snippet       Define Product Discount Percentage - WooCommerce
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */
 
add_action( 'woocommerce_product_options_pricing', 'bbloomer_set_percentage_discount' );

function bbloomer_set_percentage_discount() {
	global $product_object;
	woocommerce_wp_select(
		array(
			'id' => '_pc_discount',
			'value' => get_post_meta( $product_object->get_id(), '_pc_discount', true ),
			'label' => 'Discount %',
			'options' => array(
				'0' => '0',
				'10' => '10',
				'25' => '25',
				'50' => '50',
			),
		)
	);
}

add_action( 'save_post_product', 'bbloomer_save_percentage_discount' );

function bbloomer_save_percentage_discount( $product_id ) {
	global $typenow;
	if ( 'product' === $typenow ) {
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
		if ( isset( $_POST['_pc_discount'] ) ) {
			update_post_meta( $product_id, '_pc_discount', $_POST['_pc_discount'] );
		}
	}
}

add_filter( 'woocommerce_get_price_html', 'bbloomer_alter_price_display', 9999, 2 );

function bbloomer_alter_price_display( $price_html, $product ) {
	if ( '' === $product->get_price() ) return $price_html;
	if ( get_post_meta( $product->get_id(), '_pc_discount', true ) && get_post_meta( $product->get_id(), '_pc_discount', true ) > 0 ) {
		if ( is_admin() ) {
			return $price_html . ' <code style="white-space: nowrap">-' . get_post_meta( $product->get_id(), '_pc_discount', true ) . '% on top</code>';
		}
		$orig_price = wc_get_price_to_display( $product );
		$price_html = wc_format_sale_price( $orig_price, $orig_price * ( 100 - get_post_meta( $product->get_id(), '_pc_discount', true ) ) / 100 );
	}
	return $price_html;
}

add_action( 'woocommerce_before_calculate_totals', 'bbloomer_alter_price_cart', 9999 );

function bbloomer_alter_price_cart( $cart ) {
	if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
	if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
	foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
		$product = $cart_item['data'];
		if ( get_post_meta( $product->get_id(), '_pc_discount', true ) && get_post_meta( $product->get_id(), '_pc_discount', true ) > 0 ) {
			$price = $product->get_price();
			$cart_item['data']->set_price( $price * ( 100 - get_post_meta( $product->get_id(), '_pc_discount', true ) ) / 100 );
		}
	}
}

add_filter( 'woocommerce_product_is_on_sale', 'bbloomer_is_onsale_if_percentage_discount', 9999, 2 );

function bbloomer_is_onsale_if_percentage_discount( $on_sale, $product ) {
	if ( is_admin() ) return $on_sale;
	if ( get_post_meta( $product->get_id(), '_pc_discount', true ) && get_post_meta( $product->get_id(), '_pc_discount', true ) > 0 ) {
		$on_sale = true;
	}
	return $on_sale;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

20 thoughts on “WooCommerce: Set Product Discount Percentage @ Product Admin

  1. Would you please help me?
    Im trying to create a page that shows only products with specific percentage of discount, for example:

    Page 1: only shows products that have 20% off

    Page 2: show products that have 20% off or more up to 50%

    All products have regular price and sale price but i didn’t find any solution by searching multiple times in web!!!!???

    I appreciate if you care…

    1. Matson, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. I noticed that the SALE BADGE that I have in my theme that appears on product images when a product is on sale, the code results in this sale badge to display 0% as oppose to the my choice (which I did 50%). The sale price part works though and puts the item on sale, but still doesn’t display the % off in my sale badge.

    1. If this works with a default theme e.g. Storefront, then the problem is with your theme, and the way they coded it. In such a case, some additional code may be required to make it compatible

  3. Thank you, nice snippet!
    But I have a suffix (set in Settings >Taxes), for the prices which is this one: +IVA 10% (tot. {price_including_tax}), but your code seems to strip it away.
    Any quick fix?
    Thank you!

    1. Ciao Marzia! I think the issue is with the “bbloomer_alter_price_display” function. Try change this line:

      $price_html = wc_format_sale_price( $orig_price, $orig_price * ( 100 - get_post_meta( $product->get_id(), '_pc_discount', true ) ) / 100 );

      to:

      $price_html = wc_format_sale_price( $orig_price, $orig_price * ( 100 - get_post_meta( $product->get_id(), '_pc_discount', true ) ) / 100 ) . $product->get_price_suffix();
      1. Hello Rodolfo!
        thank you for your reply.
        I tried but the {price_including_tax} is considered on regular price, not ON THE discounted price.

        Example, this what I see in my test.:
        Regular price: €12.20.
        Discount of 20% : €9.76. + IVA 10% (tot. €13.42).

        13.42 is 12.20+10%
        Thank you!

        1. Can you show me a screenshot and explain this again please? Not sure I follow

  4. I sincerely thank you for this code, this is exactly what I was looking for!

  5. Hi,

    nice snippet, it’s possibile to add expiration date?

    Thanks

    1. Hello Domenico, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. How do i make the discount percentage appear in the sale bubble? I tried other codes but I can’t get this code to work together because the sale price is not set. Can you help me out on this?

    1. Hello Marein, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  7. Thank you so much!the code is work fine but a have a small problem the on sale product label doesn’t show like for exemple -34%

    1. Good point! Try the revised snippet please

  8. Excellent, tested with the latest version of Woocommerce and it works, too bad it’s only for simple products and not for variable products

  9. Thank you so much! This is just what I was looking for. As a small home business, I cannot afford to pay annual fees for extra plugin just to do this action. Thanks so much for sharing this!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *