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 );
}
}