问题
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.
回答1:
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>';
}
来源:https://stackoverflow.com/questions/62172918/add-a-purchase-condition-to-woocommerce-products