这并不复杂,下面是一段代码片段,可以帮助您开始:
if(is_user_logged_in()) {
//We only need these products to check
$products_to_check = array(81,82,83,84);
$customer_bought = array();
//Get all orders made by the current user in the last 365 days
$customer_orders = get_posts( apply_filters( \'woocommerce_my_account_my_orders_query\', array(
\'numberposts\' => $order_count,
\'meta_key\' => \'_customer_user\',
\'meta_value\' => get_current_user_id(),
\'post_type\' => wc_get_order_types( \'view-orders\' ),
\'post_status\' => array(\'wc-completed\', \'wc-processing\'),
\'date_query\' => array(
array(
\'column\' => \'post_date_gmt\',
\'after\' => \'1 year ago\',
)
)
)));
//Loop trough the orders
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order();
$order->populate( $customer_order );
$items = $order->get_items();
//Loop trough the order items and see if it is a product we need to cehck
foreach($items as $item) {
if(in_array($item[\'product_id\'], $products_to_check)) {
$customer_bought[] = $item[\'product_id\'];
}
}
}
} else {
echo \'You need to sign in first!\';
}
简而言之:我们确保用户已登录,我们获得当前登录客户去年的所有订单,我们循环查看订单,看看他们是否购买了我们正在寻找的特定商品。之后,您可以使用$customer\\u Buyed数组检查他们是否购买了产品,如下所示:
if(in_array(81, $customer_bought)) {
echo \'You bought the item #81 in the last 365 days, so you will get these links...\';
}
if(in_array(82, $customer_bought)) {
echo \'You bought the item #82 in the last 365 days, so you will get these links...\';
}
资料来源:
http://woahcommerce.com/2015/06/check-if-user-bought-a-specific-woocommerce-product/