问题
In woocommerce I use wc_customer_bought_product() function to display a custom text message in single product pages when customer has already bought the current product. but I would like to check that the product(s) bought are on orders with "completed" status.
The message is not supposed to be shown unless the following criteria is met:
1) user is logged in 2) user has purchased the product 3) At least one placed order for the current product is set to complete (what I cannot figure out how to get done).
Here is my code:
add_action( 'woocommerce_before_single_product_summary', 'woo_review_discount_message');
function woo_review_discount_message() {
if ( is_user_logged_in() ) {
global $product;
$current_user = wp_get_current_user();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() && $order->status->complete ) )
echo '<div class="user-bought"><span style="color:#CA364D;font-weight:bold;font-size:18px;"><i class="wishlist-icon icon-heart-o"></i></span> Hi ' . $current_user->first_name . '! Please write a review below.</a></div>';
}
}
I tried adding && $order->status->complete
but that did not work.
Any help is highly appreciated.
回答1:
If you want to target only Orders with a complete status, you can use the following custom conditional function based on wc_customer_bought_product() source code:
// 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 class="user-bought"><span style="color:#CA364D;font-weight:bold;font-size:18px;"><i class="wishlist-icon icon-heart-o"></i></span> Hi ' . $user->first_name . '! Please write a review below.</a></div>';
}
}
Code goes in function.php file of the active child theme (or active theme). Tested and works.
If customer is not logged in nothing will be displayed.
Like Woocommerce function
wc_customer_bought_product()
, this will not work on variable products single pages.
来源:https://stackoverflow.com/questions/53048413/display-a-custom-text-if-user-has-already-bought-the-current-product-in-woocomme