从第三方插件调用 Woocommerce wc_customer_bought_product 方法

Call Woocommerce wc_customer_bought_product methods from a third party plugin

在 WooCommerce 中,我正在使用 Boss Learndash 插件,并在此插件的模板文件中 plugins/boss-learndash/templates/learndash/single-sfwd-course.php,我正在尝试向已购买 course/product 的用户再次添加按钮。为此,我试图在模板中调用 wc_customer_bought_product woocommerce 函数,但似乎无法调用该函数。

我尝试添加 global $woocommerce;,也尝试添加 wc->user->wc_customer_bought_product,但无法修复它。

我做错了什么?

wc_customer_bought_product() 函数 不是任何 WooCommerce class 的方法。它只是一个条件函数,有 3 个参数 $customer_email$user_id$product_id:

wc_customer_bought_product( $customer_email, $user_id, $product_id );

它将 return 一个布尔值 truefalse,因此您将在 if 语句中使用它作为一个条件函数。

要获取用户 ID 和客户电子邮件,您可以使用:

// Get the current user data:
$user = wp_get_current_user();
$user_id = $user->ID; // Get the user ID
$customer_email = $user->user_email; // Get the user email
// OR
// $customer_email = get_user_meta( $user->ID, 'billing_email', true ); // Get the user billing email

// The conditional function (example)
// IMPORTANT: $product_id argument need to be defined
if( wc_customer_bought_product( $customer_email, $user_id, $product_id ) ) {
    echo "Has bought the product";
} else {
    echo "Has not bought the product yet";
}