6

I have previously asked this question however it does not answer the question of how to retrieve data from a coupon in WooCommerce. Both questions involve coupons, however, the first question is asking how does one set the metadata and this question asks how you retrieve the data.


I am trying to get the details of a coupon from WooCommerce by the coupon code. However, I am not sure on how I should try to go about doing this.

I have tried the code below. However, it gives me the error Call to undefined function WC_Coupon()

$coupon_code = 'save10percent';
global $woocommerce;
$c = WC_Coupon($coupon_code);

How should one go about getting the details of a coupon?

Marcello B.
  • 4,177
  • 11
  • 45
  • 65
  • 1
    You got **this answer** already is one of **your question answer**: [Set a coupon description in WooCommerce](https://stackoverflow.com/questions/47360869/set-a-coupon-description-in-woocommerce/47361312#47361312) … – LoicTheAztec Nov 27 '17 at 18:09
  • 2
    @LoicTheAztec that question is not the same as this post title and question and therefore is not duplicate! – Hamid Araghi Sep 09 '19 at 16:45
  • 1
    @HamidAraghi Sorry but the way to get the data is the same, using `WC_Coupon` **getter** methods instead of **setter** methods that this OP asked before… So in both cases you use `WC_Coupon` class methods on the `WC_Coupon` instance object. In My linked answer, I have showed how to get the `WC_Coupon` object and I have added the link to `WC_Coupon` Class available methods… This has also been answered in other threads on how to get coupon data (from orders for example, which is the same process, using `WC_Coupon` getter methods on the `WC_Coupon` Object). – LoicTheAztec Sep 12 '19 at 18:19

1 Answers1

19

I figured it out. In order for the WC_Coupon function to work, I needed to add the "new" keyword prior to calling the function. As demonstrated below.

$coupon_code = 'save10percent';
global $woocommerce;
$c = new WC_Coupon($coupon_code);

Now I can get details about the coupon like so

echo "Discount Amount ".$c->amount."<br>";//Get Discount amount
echo "Discount Type ".$c->discount_type."<br>";//Get type of discount
echo "Individual Use ".$c->individual_use."<br>";//Get individual use status
echo "Usage Count ".$c->usage_count."<br>";//Get number of times the coupon has been used
echo "Uage Limit ".$c->usage_limit."<br>";//Get usage limit
echo "Coupon Description ".$c->description."<br>";//Get coupon description
Marcello B.
  • 4,177
  • 11
  • 45
  • 65
  • 7
    Depending of your context, you cannot access the object directly. Instead, you need to use woocommerce helpers eg: $c->get_amount() – Jeff Monteiro Jul 28 '20 at 17:52
  • As @JeffMonteiro said using WooCommerce functions is better I think too. This is what I used. ```'amount' => $c->get_amount(), 'date_expires' => $c->get_date_expires(), 'discount_type' => $c->get_discount_type(), 'product_ids' => $c->get_product_ids(), 'excluded_product_ids' => $c->get_excluded_product_ids(), 'usage_count' => $c->get_usage_count(), 'usage_limit' => $c->get_usage_limit(),``` Complete list is available here - https://woocommerce.github.io/code-reference/classes/WC-Coupon.html Thanks OP for the original answer. – Arun Basil Lal Nov 24 '22 at 04:10
  • global $woocommerce; is not needed for new WC_Coupon( $coupon_code ), is it? – jMike Aug 12 '23 at 08:25