5

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?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Rubén
  • 53
  • 4
  • Welcome to Stack Overflow! Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. Questions without a **clear problem statement** are not useful to other readers. See: [How to create a Minimal, Reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – rizerphe Jun 23 '20 at 18:46

1 Answers1

3

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399