dq_609586475 2017-11-27 17:56
浏览 23

从优惠券代码WooCommerce [重复]中检索优惠券详细信息

This question already has an answer here:

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?

  • 写回答

1条回答 默认 最新

  • dongliao9018 2017-11-27 17:56

    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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写
  • ¥15 graphpad prism 三因素重复测定报错