Add a purchase condition to WooCommerce products

◇◆丶佛笑我妖孽 提交于 2021-02-19 09:13:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!