[英]woocommerce, loop of all products purchased by the user
我創建了附加循環,以便能夠查看用戶購買的所有產品。 但是,沒有顯示重復的產品。 如何更改所有內容以顯示重復的產品? 有大佬幫幫我嗎? 先感謝您。
<?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();
?>
我建議從訂單開始。
也可以在數組中獲取不同的數據。 檢查代碼部分中的鏈接 // Get and Loop Over Order Items
像這樣的東西。 扁平化提供了一個很好的干凈的產品 ID 數組。 包括重復。 在最新版本的 WooCommerce 上測試。
請注意,您還可以將 wc_get_orders 的 arguments 調整為僅包含付費狀態。 在這里查看詳細信息。
$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)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.