我想在結帳頁面上顯示一條訊息,通知客戶他們過去曾購買過此產品(他們將要購買的產品),但此訊息僅在滿足這些條件時才會運行。
- 客戶必須登錄
- 用戶角色是管理員或客戶
- 之前購買的產品仍應具有“正在處理”的訂單狀態。
到目前為止,我已經能夠使前兩個條件正常作業:
function user_logged_in_product_already_bought() {
global $woocommerce;
if ( ! is_user_logged_in() ) return;
$items = $woocommerce->cart->get_cart();
$has_bought = false;
foreach($items as $item => $values) {
if ( wc_customer_bought_product( '', get_current_user_id(), $values['data']->get_id() ) ) {
$has_bought = true;
break;
}
}
$user = wp_get_current_user();
$allowed_roles = array( 'administrator', 'customer' );
if ( array_intersect( $allowed_roles, $user->roles ) ) {
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' );
注意:我使用的原因foreach是用戶一次只能購買一種產品(購物車中不能有多個預約產品)
但我不知道如何處理第三個條件。有什么建議嗎?
uj5u.com熱心網友回復:
您的前 2 個步驟確實有效,對于第 3 步,您必須使用自定義函式,該函式僅檢查訂單狀態“正在處理”。
此自定義功能的優勢在于,與處理所有現有訂單相比,它速度更快、重量更輕。
所以你得到:
function has_bought_items( $user_id = 0, $product_ids = 0 ) {
// When empty, return false
if ( empty ( $product_ids ) ) return false;
global $wpdb;
$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-processing' )
AND pm.meta_key = '_customer_user'
AND pm.meta_value = '$user_id'
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;
}
function action_woocommerce_before_checkout_form() {
// Customer must be logged in
if ( ! is_user_logged_in() ) return;
// Get current user
$user = wp_get_current_user();
// Allowed user roles
$allowed_roles = array( 'administrator', 'customer' );
// Compare
if ( array_intersect( $allowed_roles, $user->roles ) ) {
// WC Cart NOT null
if ( ! is_null( WC()->cart ) ) {
// Initialize
$product_ids = array();
// Loop through cart contents
foreach ( WC()->cart->get_cart_contents() as $cart_item ) {
// Get product ID and push to array
$product_ids[] = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
}
// Call function, and if true
if ( has_bought_items( $user->ID, $product_ids ) ) {
// Notice
wc_print_notice( __( 'You purchased this in the past. Buy again?', 'woocommerce' ), 'success' );
}
}
}
}
add_action( 'woocommerce_before_checkout_form', 'action_woocommerce_before_checkout_form' );
結果:一般訊息

可選:不是顯示一般訊息,而是按產品單獨顯示,您可以使用woocommerce_checkout_cart_item_quantity鉤子
所以你會得到:
function has_bought_items( $user_id = 0, $product_ids = 0 ) {
// When empty, return false
if ( empty ( $product_ids ) ) return false;
global $wpdb;
$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-processing' )
AND pm.meta_key = '_customer_user'
AND pm.meta_value = '$user_id'
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;
}
function filter_woocommerce_checkout_cart_item_quantity( $item_qty, $cart_item, $cart_item_key ) {
// Customer must be logged in
if ( ! is_user_logged_in() ) return;
// Get current user
$user = wp_get_current_user();
// Allowed user roles
$allowed_roles = array( 'administrator', 'customer' );
// Initialize
$message = '';
// Compare
if ( array_intersect( $allowed_roles, $user->roles ) ) {
// Get product id
$product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
// Call function, and if true
if ( has_bought_items( $user->ID, $product_id ) ) {
$message = '<p>' . __( 'You purchased this in the past. Buy again?', 'woocommerce' ) . '</p>';
}
}
// Return
return $item_qty . $message;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'filter_woocommerce_checkout_cart_item_quantity', 10, 3 );
結果:按產品分開顯示

注:該has_bought_items()功能基于WooCommerce答案代碼中檢查用戶是否購買了特定產品
相關: 如果用戶之前購買過產品,則在 WooCommerce 購物車頁面上的產品名稱下方顯示訊息
uj5u.com熱心網友回復:
根據這篇文章:
您可以實作如下登錄:
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user->ID,
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_is_paid_statuses() ),
) );
// LOOP THROUGH ORDERS AND GET PRODUCT IDS
if ( ! $customer_orders ) return;
$product_ids = array();
foreach($items as $item) {
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order->ID );
$orderItems = $order->get_items();
foreach ( $orderItems as $orderItem ) {
if ($orderItem->get_product_id() == $item->get_product_id() )
$has_bought = true;
break;
}
}
}
uj5u.com熱心網友回復:
關于第三點,您可以使用輕松獲得客戶處理訂單 wc_get_orders()
$user = wp_get_current_user();
$processing_orders = wc_get_orders(
array(
'customer' => $user->ID,
'status' => 'wc-processing',
)
);
您可以檢查處理訂單是否為空。您可以遍歷訂單產品以獲取有關每個產品的詳細資訊。
foreach ( $processing_orders as $p_order ) {
$order_products = $p_order->get_items();
foreach ( $order_products as $product_item ) {
$product_id = $product_item->get_id();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/403122.html
標籤:
上一篇:如何跟蹤遞回函式被呼叫的次數
