【问题标题】:Call Woocommerce wc_customer_bought_product methods from a third party plugin从第三方插件调用 Woocommerce wc_customer_bought_product 方法
【发布时间】:2017-10-23 13:03:07
【问题描述】:
在 WooCommerce 中,我正在使用 Boss Learndash 插件,并且在此插件 plugins/boss-learndash/templates/learndash/single-sfwd-course.php 的模板文件中,我正在尝试向购买了课程/产品的用户再次添加按钮。为此,我试图在模板中调用 wc_customer_bought_product woocommerce 函数,但似乎无法调用该函数。
我尝试添加global $woocommerce;,也尝试添加wc->user->wc_customer_bought_product,但无法修复。
我做错了什么?
【问题讨论】:
标签:
php
wordpress
woocommerce
product
user-data
【解决方案1】:
wc_customer_bought_product() 函数不是任何 WooCommerce 类的方法。它只是一个带有 3 个参数的条件函数 $customer_email、$user_id 和 $product_id:
wc_customer_bought_product( $customer_email, $user_id, $product_id );
它将返回布尔值true 或false,因此您将在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";
}