简体   繁体   English

为WooCommerce产品添加购买条件

[英]Add a purchase condition to WooCommerce products

I ma trying to add a purchase condition to my WooCommerce products with the following:我正在尝试使用以下内容向我的 WooCommerce 产品添加购买条件:

{$current_user = wp_get_current_user();

if ( current_user_can('administrator') || wc_customer_bought_product($current_user->email, $current_user->ID,
// return true
return true;}

But I don't know if this code is correct and an advise will be helpful.但我不知道这段代码是否正确,建议会有所帮助。

The product Id argument is missing from wc_customer_bought_product() function, and you can get more easily the WP_User object. wc_customer_bought_product() function 中缺少产品 ID 参数,您可以更轻松地获得WP_User object。 Here is a usage example:这是一个使用示例:

global $current_user;

if ( is_user_logged_in() && wc_customer_bought_product( $current_user->email, $current_user->ID, get_the_id() ) ) {
    // Do something
    echo '<p>' . __("You have already purchased this product before") . '</p>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM