Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_shortcode('trending_products_of_the_week', function($atts) {
- $all_orders = wc_get_orders(
- array(
- 'limit' => -1,
- 'status' => wc_get_is_paid_statuses(),
- 'date_after' => date( 'Y-m-d', strtotime( '-7 days' ) ),
- 'return' => 'ids',
- )
- );
- $trending = array();
- foreach ( $all_orders as $all_order ) {
- $order = wc_get_order( $all_order );
- $items = $order->get_items();
- foreach ( $items as $item ) {
- $product_id = $item->get_product_id();
- if ( ! $product_id ) continue;
- $trending[$product_id] = $trending[$product_id] ? (int) $trending[$product_id] + $item['qty'] : $item['qty'];
- }
- }
- arsort( $trending, SORT_NUMERIC );
- $ids = array_keys( array_slice( $trending, 0, 10, true ) );
- return do_shortcode('[products ids="' . implode(',', $ids) . '"]');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement