【问题标题】:Retrieve coupon details from coupon code WooCommerce [duplicate]从优惠券代码 WooCommerce 中检索优惠券详细信息 [重复]
【发布时间】:2018-05-11 01:16:44
【问题描述】:

我之前曾问过this question,但它没有回答如何从 WooCommerce 中的优惠券中检索数据的问题。这两个问题都涉及优惠券,但是,first question 询问如何设置元数据,而此问题询问您如何检索数据。


我正在尝试通过优惠券代码从 WooCommerce 获取优惠券的详细信息。但是,我不确定我应该如何尝试这样做。

我已经尝试了下面的代码。但是,它给了我错误Call to undefined function WC_Coupon()

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

如何获取优惠券的详细信息?

【问题讨论】:

  • 你得到的这个答案已经是你的问题答案之一:Set a coupon description in WooCommerce ...
  • @LoicTheAztec 该问题与此帖子标题和问题不同,因此不重复!
  • @HamidAraghi 抱歉,但获取数据的方法是相同的,使用 WC_Coupon getter 方法而不是此 OP 要求的 setter 方法之前……所以在这两种情况下,您都在WC_Coupon 实例对象上使用WC_Coupon 类方法。在我的链接答案中,我展示了如何获取 WC_Coupon 对象,并添加了指向 WC_Coupon 类可用方法的链接......这也已在其他线程中得到解答,关于如何获取优惠券数据(例如,来自订单,这是相同的过程,在WC_Coupon 对象上使用WC_Coupon getter 方法)。

标签: php wordpress woocommerce coupon


【解决方案1】:

我想通了。为了使WC_Coupon 函数起作用,我需要在调用函数之前添加“new”关键字。如下所示。

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

现在我可以像这样获取有关优惠券的详细信息

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

【讨论】:

  • 根据您的上下文,您不能直接访问该对象。相反,您需要使用 woocommerce 助手,例如:$c->get_amount()
猜你喜欢
  • 2014-11-26
  • 2017-05-26
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 2019-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多