7

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?

3
  • 1
    You got this answer already is one of your question answer: Set a coupon description in WooCommerce Commented Nov 27, 2017 at 18:09
  • 2
    @LoicTheAztec that question is not the same as this post title and question and therefore is not duplicate! Commented Sep 9, 2019 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). Commented Sep 12, 2019 at 18:19

1 Answer 1

23

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
3
  • 7
    Depending of your context, you cannot access the object directly. Instead, you need to use woocommerce helpers eg: $c->get_amount() Commented Jul 28, 2020 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 - woocommerce.github.io/code-reference/classes/WC-Coupon.html Thanks OP for the original answer. Commented Nov 24, 2022 at 4:10
  • global $woocommerce; is not needed for new WC_Coupon( $coupon_code ), is it?
    – jMike
    Commented Aug 12, 2023 at 8:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.