从优惠券代码 WooCommerce 中检索优惠券详细信息

Sui*_*pps 5 php wordpress coupon woocommerce

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


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

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

$coupon_code = 'save10percent';
global $woocommerce;
$c = WC_Coupon($coupon_code);
Run Code Online (Sandbox Code Playgroud)

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

Sui*_*pps 15

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

$coupon_code = 'save10percent';
global $woocommerce;
$c = new WC_Coupon($coupon_code);
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

  • 根据您的上下文,您无法直接访问该对象。相反,您需要使用 woocommerce 助手,例如: $c-&gt;get_amount() (7认同)