Example #1
0
 /**
  * Split order into vendor orders (when applicable) after checkout
  *
  * @since 
  * @param int $order_id
  * @return void
  */
 public static function create_child_orders($order_id)
 {
     $order = wc_get_order($order_id);
     $items = $order->get_items();
     $vendor_items = array();
     foreach ($items as $item_id => $item) {
         if (isset($item['product_id']) && $item['product_id'] !== 0) {
             // check if product is from vendor
             $product_author = get_post_field('post_author', $item['product_id']);
             if (WCV_Vendors::is_vendor($product_author)) {
                 $vendor_items[$product_author][$item_id] = array('item_id' => $item_id, 'qty' => $item['qty'], 'total' => $item['line_total'], 'subtotal' => $item['line_subtotal'], 'tax' => $item['line_tax'], 'subtotal_tax' => $item['line_subtotal_tax'], 'tax_data' => maybe_unserialize($item['line_tax_data']), 'commission' => WCV_Commission::calculate_commission($item['line_subtotal'], $item['product_id'], $order, $item['qty']));
             }
         }
     }
     foreach ($vendor_items as $vendor_id => $items) {
         if (!empty($items)) {
             $vendor_order = WCV_Vendors::create_vendor_order(array('order_id' => $order_id, 'vendor_id' => $vendor_id, 'line_items' => $items));
         }
     }
 }