所以我有这个wordpress插件,它本身隐藏了“添加到购物车”按钮,并在 Woocommerce产品免费时添加了一个下载按钮。
但我想在此插件中添加另一个功能,在购买产品时应该这样做。
所以我正在尝试使用wc_customer_bought_product( $customer_email, $user_id, $the_product)
woocommerce功能。
但它似乎没有起作用。
//Returns true if the product/variation is free, downloadable and vitual.
//Added by me: OR return true too if product was bought
function dfd_is_virtual_free_download_product($post) {
global $woocommerce, $product;
$user_id = get_current_user_id();
$current_user= wp_get_current_user();
$customer_email = $current_user->email;
$current_product = $product->id;
if (empty($post)) return false;
$the_product = get_product($post);
if (!$the_product) {
//its not a product or product not found
return false;
}
$is_virtual = $the_product->is_virtual();
$price = $the_product->regular_price;
$sale_price = $the_product->sale_price;
$is_downloadable = $the_product->is_downloadable();
if ($sale_price == "0" || $sale_price == "0.00" || wc_customer_bought_product( $customer_email, $user_id, $current_product)){
$price = $sale_price;
}
if($price == 0 && $is_virtual == 1 && $is_downloadable == 1){
return true;
}
return false;
}
答案 0 :(得分:0)
我在您的代码中发现了错误。 为什么要两次获取ID?
$user_id = get_current_user_id();
$current_user= wp_get_current_user();
$ current_user已注入此属性,您可以通过这种方式调用$ current_user-> ID
另一个问题是调用未定义的属性$ current_user-> email
//This is wrong
$customer_email = $current_user->email;
//This works
$customer_email = $current_user->user_email;
希望这有帮助。
尝试以这种方式调用函数:
wc_customer_bought_product( $customer_email, (int)$user_id, (int)$current_product);
您最好尝试使用var_dump()来获取有关值的信息或仅使用调试器。