为WooCommerce产品添加购买条件

问题描述 投票:1回答:2

我正在尝试通过以下方式向我的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;}

但是我不知道此代码是否正确,建议会有所帮助。

php wordpress woocommerce product user-data
2个回答
0
投票

wc_customer_bought_product()函数中缺少产品ID参数,您可以更轻松地获得WP_User对象。这是一个用法示例:

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>';
}

0
投票

非常感谢你,是这个<3

© www.soinside.com 2019 - 2024. All rights reserved.