Search code examples
phpwordpresswoocommerceproductcoupon

Display a coupon code on single product pages in Woocommerce


I want to display discount coupon on every product page on woocommerce for two reasons.

  1. A lot of new customers overlooks discount coupons mentioned on home page and header of website despite my best intention.

  2. With discount coupons I want to encourage more users to make prepaid payments and avoid Cash on Delivery.

Is this something possible to achieve?


Solution

  • Update: Added the hook for cart page…

    This can be done simply with the following, that will add a custom notice message with the coupon code in single product pages:

    add_action( 'woocommerce_before_single_product', 'custom_message_before_single_product', 5 ); // For single product page
    add_action( 'woocommerce_before_cart', 'custom_message_before_single_product'); // For cart page
    function custom_message_before_single_product() {
        $coupon_code = 'special10';
    
        wc_print_notice( sprintf(
            __("You get %s when using credit card payment with the %s coupon code.", "woocommerce"),
            '<strong>' . __("a 10% discount", "woocommerce") . '</strong>',
            '<code>' . $coupon_code . '</code>'
        ), 'notice' );
    }
    

    Code goes in function.php file of your active child theme (or active theme). tested and works.

    You will get something like this in your single product pages:

    enter image description here