Advertisement
palsushobhan

trending_products_of_the_week

Oct 22nd, 2024
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.90 KB | None | 0 0
  1. add_shortcode('trending_products_of_the_week', function($atts) {
  2.     $all_orders = wc_get_orders(
  3.         array(
  4.            'limit' => -1,
  5.            'status' => wc_get_is_paid_statuses(),
  6.            'date_after' => date( 'Y-m-d', strtotime( '-7 days' ) ),
  7.            'return' => 'ids',
  8.         )
  9.     );
  10.     $trending = array();
  11.     foreach ( $all_orders as $all_order ) {
  12.         $order = wc_get_order( $all_order );
  13.         $items = $order->get_items();
  14.         foreach ( $items as $item ) {
  15.             $product_id = $item->get_product_id();
  16.             if ( ! $product_id ) continue;
  17.             $trending[$product_id] = $trending[$product_id] ? (int) $trending[$product_id] + $item['qty'] : $item['qty'];
  18.         }
  19.     }
  20.     arsort( $trending, SORT_NUMERIC );
  21.     $ids = array_keys( array_slice( $trending, 0, 10, true ) );
  22.     return do_shortcode('[products ids="' . implode(',', $ids) . '"]');
  23. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement