I have a WooCommerce website for digital products. The products that sold generally have a custom tag named "Recently Sold". But, I want to show all the sold items in a specific page where only the sold items will be displayed.
I got a plugin "WooCommerce Toolkit" Which show recently sold items in the sidebar and another is "Woo recently sold items list" which show recently sold item only in the single product page.
I want to show all the sold items on a page like "Sold" page.
I hope it now makes sense.
add_shortcode( 'sold_products', 'wc_products_allsold' );
function wc_products_allsold() {
// Get last week orders
$all_orders = wc_get_orders(
array(
'limit' => -1,
'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ),
'return' => 'ids',
)
);
// Sum quantities purchased
$count = 0;
$produt_sale_list = array();
foreach ( $all_orders as $all_order ) {
$order = wc_get_order( $all_order );
$items = $order->get_items();
foreach ( $items as $item ) {
$produt_sale_list[ $item->get_name() ][] = $item[ 'qty' ];
$count = $count + absint( $item[ 'qty' ] );
}
}
foreach ( $produt_sale_list as $produt_sold => $value ) {
echo "<p>$produt_sold sold: " . array_sum( $value ) . "</p>";
}
if ( $count > 0 )
echo "<p>Recent sales: $count</p>";
}
Add the shortcode [sold_products]
into the page where you want to display all sold products.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.