Search code examples
wordpresswoocommercecartcoupon

Display coupon description woocommerce


I am trying to display coupon description once the coupon is applied (10%) in cart page.

To display Total I am using $woocommerce->cart->cart_contents_total

How do I display coupon description?


Solution

  • As you have not mentioned where do you want to have coupon description, I have printed it just before Cart Total.

    If you want to have it on different place, you can modify action. You can find it from here.

    Code:

        add_action('woocommerce_before_cart_totals', 'apply_product_on_coupon');
        function apply_product_on_coupon() {
            global $woocommerce;
    
            if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
                 $my_coupon = $woocommerce->cart->get_coupons() ;
                 foreach($my_coupon as $coupon){
    
                    if ( $post = get_post( $coupon->id ) ) {
                            if ( !empty( $post->post_excerpt ) ) {
                                echo "<span class='coupon-name'><b>".$coupon->code."</b></span>";
                                echo "<p class='coupon-description'>".$post->post_excerpt."</p>";
                            }
                    }
                }
            }
        }
    

    Let me know if you have any doubts.