简体   繁体   中英

Add a purchase condition to WooCommerce products

I ma trying to add a purchase condition to my WooCommerce products with the following:

{$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. 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>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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