我創建了附加的回圈,以便能夠查看用戶購買的所有產品。但是,沒有顯示重復的產品。如何更改所有內容以顯示重復的產品?有大佬幫幫我嗎?先感謝您。
<?php
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
$customer_email = $current_user->email;
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
);
$loop = new WP_Query($args);
if ($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post();
if(wc_customer_bought_product($customer_email, $user_id, get_the_ID())) {
global $product;
$id = $product->get_id();
?>
uj5u.com熱心網友回復:
我建議從訂單開始。
- 獲取用戶的所有訂單
- 遍歷來自該用戶的所有訂單并創建所有產品的陣列
- 然后將陣列展平,您就可以使用產品了。
也可以在陣列中獲取不同的資料。檢查鏈接,在代碼部分 // Get and Loop Over Order Items
像這樣的東西。扁平化提供了一個很好的干凈的產品 ID 陣列。包括重復。在最新版本的 WooCommerce 上測驗。
請注意,您還可以將 wc_get_orders 的引數調整為僅包含付費狀態。檢查這里的詳細資訊。
$orders = wc_get_orders(array(
'customer_id' => get_current_user_id(),
));
// should be an array of objects
foreach($orders as $order) :
// loop through the orders
foreach ( $order->get_items() as $item_id => $item ) :
$ids[] = $item->get_product_id();
endforeach;
// add all ids in a new loop
$AllIds[] = $ids;
endforeach;
// flattens the array nicely.
$product_ids = array_merge(...array_values(($AllIds)));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/377582.html
標籤:php WordPress的 求购
上一篇:將Wordpress搜索限制為特定的博客類別不起作用
下一篇:基于ACF值的入隊腳本
