Search code examples
phpwordpresswoocommerceemail-notificationscoupon

Get coupon description From WooCommerce order applied coupons


I'm trying to display used coupons on WooCommerce order emails + add THE DESCRIPTION.

Displaying coupons is working based on: Add Applied Coupon Code in Admin New Order Email Template - WooCommerce

I also tried this:

$coupons = $order->get_items( 'coupon' );
  foreach ( $coupons as $item_id => $item ) {
    echo "<span class='coupon-name'><b>".$item['name']."</b></span>";
    $post = get_post( $item_id );
    echo "<p class='coupon-description'>".$post->post_excerpt."</p>";
  }
}

But is not working... any idea?


Solution

  • Use the following to get the coupon description from "coupon" order items:

    // Loop through WC_Order_Item_Coupon Objects
    foreach ( $order->get_items( 'coupon' ) as $item ) {
        // Get the WC_Coupon Object
        $coupon = new WC_Coupon($item->get_code());
        
        // Display coupon description
        echo "<p class='coupon-description'>".$coupon->get_description()."</p>";
    }
    

    Related: Get coupon data from WooCommerce orders