如果您 只 希望定位 具有完整状态的订单
,则可以基于
wc_customer_bought_product()源代码使用以下自定义条件函数:
// Utility function to check if a customer has bought a product (Order with "completed" status only)function customer_has_bought_product( $product_id, $user_id = 0 ) { global $wpdb; $customer_id = $user_id == 0 || $user_id == '' ? get_current_user_id() : $user_id; $status = 'wc-completed'; if( ! $customer_id ) return false; // Count the number of products $count = $wpdb->get_var( " SELECt COUNT(woim.meta_value) FROM {$wpdb->prefix}posts AS p INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id WHERe p.post_status = '$status' AND pm.meta_key = '_customer_user' AND pm.meta_value = $customer_id AND woim.meta_key IN ('_product_id','_variation_id') AND woim.meta_value = $product_id " ); // Return a boolean value if count is higher than 0 return $count > 0 ? true : false;}add_action( 'woocommerce_before_single_product_summary', 'woo_review_discount_message');function woo_review_discount_message() { global $product; if ( customer_has_bought_product( $product->get_id() ) && ! $product->is_type('variable') ) { $user = wp_get_current_user(); echo '<div ><span ><i ></i></span> Hi ' . $user->first_name . '! Please write a review below.</a></div>'; }}代码进入活动子主题(或活动主题)的function.php文件中。经过测试和工作。
如果客户未登录,则不会显示任何内容。
像Woocommerce功能一样
wc_customer_bought_product(),这不适用于可变产品的单个页面。



