在我的網站上,如果用戶之前購買了他們將要購買的產品,我想在結帳頁面上顯示一條自定義訊息。所以我遇到了這段代碼,如果用戶已經購買了該產品,它實際上會顯示一條自定義訊息,該代碼僅顯示在單個產品頁面上。請問如何在結帳頁面上顯示此訊息?
add_action( 'woocommerce_before_add_to_cart_form', 'user_logged_in_product_already_bought', 30 );
function user_logged_in_product_already_bought() {
global $product;
if ( ! is_user_logged_in() ) return;
if ( wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
echo '<div>You purchased this in the past. Buy again?</div>';
}
}
uj5u.com熱心網友回復:
根據@Dmitry建議,您可以使用woocommerce_before_checkout_form動作掛鉤。但您無法global $product;在結帳頁面上訪問。試試下面的代碼。
function user_logged_in_product_already_bought() {
global $woocommerce;
if ( ! is_user_logged_in() ) return;
// check if current user in specific role.
$user = wp_get_current_user();
if ( in_array( 'customer', (array) $user->roles ) ) {
}
$items = $woocommerce->cart->get_cart();
$has_bought = false;
foreach($items as $item => $values) {
if ( has_bought_items( get_current_user_id(), $values['data']->get_id() ) ) {
$has_bought = true;
break;
}
}
if( $has_bought ){
wc_print_notice( "You purchased this in the past. Buy again?", 'success' );
}
}
add_action( 'woocommerce_before_checkout_form', 'user_logged_in_product_already_bought' );
測驗和作業

根據@7uc1f3r評論, I will like to prevent the double purchase of product per user.您可以使用woocommerce_add_to_cart_validation操作掛鉤來防止從購物車中添加產品。
function prevent_from_adding_cart_if_user_product_already_bought( $passed, $product_id, $quantity ) {
if ( has_bought_items( get_current_user_id(), $product_id ) ) {
wc_add_notice( __( "You purchased this in the past. Buy again?", 'woocommerce' ), 'error' );
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'prevent_from_adding_cart_if_user_product_already_bought', 10, 3 );
測驗和作業

您可以使用LoicTheAztec制作的這個很棒的功能來檢查用戶是否購買了特定產品。
function has_bought_items( $user_var = 0, $product_ids = 0 ) {
global $wpdb;
// Based on user ID (registered users)
if ( is_numeric( $user_var) ) {
$meta_key = '_customer_user';
$meta_value = $user_var == 0 ? (int) get_current_user_id() : (int) $user_var;
}
// Based on billing email (Guest users)
else {
$meta_key = '_billing_email';
$meta_value = sanitize_email( $user_var );
}
$paid_statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
$product_ids = is_array( $product_ids ) ? implode(',', $product_ids) : $product_ids;
$line_meta_value = $product_ids != ( 0 || '' ) ? 'AND woim.meta_value IN ('.$product_ids.')' : 'AND woim.meta_value != 0';
// Count the number of products
$count = $wpdb->get_var( "
SELECT COUNT(p.ID) 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 IN ( 'wc-" . implode( "','wc-", $paid_statuses ) . "' )
AND pm.meta_key = '$meta_key'
AND pm.meta_value = '$meta_value'
AND woim.meta_key IN ( '_product_id', '_variation_id' ) $line_meta_value
" );
// Return true if count is higher than 0 (or false)
return $count > 0 ? true : false;
}
根據 OP 請求更新。
您可以使用以下代碼 check if the current user is in a specific role.
$user = wp_get_current_user();
if ( in_array( 'customer', (array) $user->roles ) ) {
}
并且只有狀態為 'processing in has_bought_itemsfunction 的產品會更改$paid_statuses變數的值。根據您的需要。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/385214.html
標籤:php WordPress的 求购
