添加woocommerce购买的产品功能到插件


Adding a woocommerce bought product function to a plugin

所以我有这个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;
}

我在你的代码中发现了错误。你为什么要办理两次身份证?

$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()来获取有关值的信息,或者只是调试器。