WooCommerce: Apply Coupon @ Single Product Page

In WooCommerce, applying a coupon code is often part of the checkout flow, but wouldn’t it be convenient for customers to select a coupon directly on the product page before adding items to their cart?

By letting users apply discounts right at the start, you’re streamlining the shopping experience and increasing the likelihood of conversions.

In this post, we’ll show you a quick and effective code snippet to add this functionality to your WooCommerce store. With just a few lines, you’ll empower customers to apply their favorite discount on the product page itself, making the shopping process faster and more engaging.

This guide is perfect for store owners looking to improve user experience and simplify coupon management. Let’s dive in and get this feature running on your product pages!

PHP Snippet: Select & Apply Coupon Upon Add to Cart @ WooCommerce Single Product Page

Credits go to Kishore from upnrunn for sharing this on Twitter – and me for picking it up!

/**
 * @snippet       Apply Coupon From Add to Cart @ Woo Single Product
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     businessbloomer.club
 */

add_action( 'woocommerce_after_add_to_cart_button', 'bbloomer_add_coupon_radio_button', 10 );

function bbloomer_add_coupon_radio_button() {
    echo '<div style="clear:both"><br><p><label for="apply_coupon"><input type="checkbox" id="apply_coupon" name="add_coupon"> Apply a 10% discount!</label></p>';
}

add_action( 'woocommerce_add_to_cart', 'bbloomer_apply_coupon_on_checkbox_clicked', 10, 6 );

function bbloomer_apply_coupon_on_checkbox_clicked( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    if ( isset( $_POST['add_coupon'] ) ) {
       	$coupon_code = 'a862d29963f77bfb'; // COUPON CODE
       	WC()->cart->apply_coupon( $coupon_code );
    }
}

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

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 *