/** * */ public function display_shortcodes() { if (is_page(WC_Vendors::$pv_options->get_option('orders_page')) && $this->can_view_orders) { wp_enqueue_script('jquery'); $this->product_id = !empty($_GET['orders_for_product']) ? (int) $_GET['orders_for_product'] : false; $products = array($this->product_id); $_product = get_product($this->product_id); $children = $_product->get_children(); if (!empty($children)) { $products = array_merge($products, $children); $products = array_unique($products); } $this->orders = WCV_Queries::get_orders_for_products($products, array('vendor_id' => get_current_user_id())); add_action('init', array($this, 'verify_order_access')); add_shortcode('wcv_orders', array($this, 'display_product_orders')); if ($this->can_export_csv && !empty($_POST['export_orders'])) { $this->download_csv(); } } }
/** * Format products for easier displaying * * @param object $products * * @return array */ public function format_product_details($products) { if (empty($products)) { return false; } $orders_page = get_permalink(WC_Vendors::$pv_options->get_option('orders_page')); $default_commission = WC_Vendors::$pv_options->get_option('default_commission'); $total_qty = $total_cost = 0; $data = array('products' => array(), 'total_qty' => '', 'total_cost' => ''); foreach ($products as $product) { $ids[] = $product->ID; } $orders = WCV_Queries::sum_orders_for_products($ids, array('vendor_id' => get_current_user_id())); if ($orders) { foreach ($orders as $order_item) { if ($order_item->qty < 1) { continue; } $commission_rate = WCV_Commission::get_commission_rate($order_item->product_id); $_product = get_product($order_item->product_id); $id = !empty($_product->parent->id) ? $_product->parent->id : $order_item->product_id; $data['products'][$id] = array('id' => $id, 'title' => $_product->get_title(), 'qty' => !empty($data['products'][$id]) ? $data['products'][$id]['qty'] + $order_item->qty : $order_item->qty, 'cost' => !empty($data['products'][$id]) ? $data['products'][$id]['cost'] + $order_item->line_total : $order_item->line_total, 'view_orders_url' => esc_url(add_query_arg('orders_for_product', $id, $orders_page)), 'commission_rate' => $commission_rate); $total_qty += $order_item->qty; $total_cost += $order_item->line_total; } } $data['total_qty'] = $total_qty; $data['total_cost'] = $total_cost; // Sort by product title if (!empty($data['products'])) { usort($data['products'], array($this, 'sort_by_title')); } return $data; }
/** * Get Orders to display in admin * * @return $orders */ function get_orders() { $user_id = get_current_user_id(); $orders = array(); $vendor_products = $this->get_vendor_products($user_id); $products = array(); foreach ($vendor_products as $_product) { $products[] = $_product->ID; } $_orders = $this->get_orders_for_vendor_products($products); $model_id = 0; if (!empty($_orders)) { foreach ($_orders as $order) { $order = new WC_Order($order->order_id); $valid_items = WCV_Queries::get_products_for_order($order->id); $valid = array(); $items = $order->get_items(); foreach ($items as $key => $value) { if (in_array($value['variation_id'], $valid_items) || in_array($value['product_id'], $valid_items)) { $valid[] = $value; } } $products = ''; foreach ($valid as $key => $item) { $item_meta = new WC_Order_Item_Meta($item['item_meta']); // $item_meta = $item_meta->display( false, true ); $item_meta = $item_meta->get_formatted(); $products .= '<strong>' . $item['qty'] . ' x ' . $item['name'] . '</strong><br />'; foreach ($item_meta as $key => $meta) { // Remove the sold by meta key for display if (strtolower($key) != 'sold by') { $products .= $meta['label'] . ' : ' . $meta['value'] . '<br />'; } } } $shippers = (array) get_post_meta($order->id, 'wc_pv_shipped', true); $shipped = in_array($user_id, $shippers) ? 'Yes' : 'No'; $sum = WCV_Queries::sum_for_orders(array($order->id), array('vendor_id' => get_current_user_id())); $total = $sum[0]->line_total; $comment_output = ''; // Need to fix how form is submitted for adding comments if at all possible. if ($this->can_view_comments) { $order_notes = $order->get_customer_order_notes(); $comment_output .= '<a href="#TB_inline?width=600&height=550&inlineId=order-comment-window-' . $model_id . '" class="thickbox">'; $comment_output .= sprintf(__('Comments (%s)', 'wcvendors'), count($order_notes)); $comment_output .= '</a>'; $comment_output .= '<div id="order-comment-window-' . $model_id . '" style="display:none;">'; $comment_output .= '<h3>' . __('Comments to Customer', 'wcvendors') . '</h3>'; if (!empty($order_notes)) { foreach ($order_notes as $order_note) { $last_added = human_time_diff(strtotime($order_note->comment_date_gmt), current_time('timestamp', 1)); $comment_output .= '<p>'; $comment_output .= $order_note->comment_content; $comment_output .= '<br />'; $comment_output .= sprintf(__('added %s ago', 'wcvendors'), $last_added); $comment_output .= '<br />'; $comment_output .= '</p>'; } } else { $comment_output .= '<p>' . __('No comments currently to customer.', 'wcvendors') . '</p>'; } if ($this->can_add_comments) { $comment_output .= wp_nonce_field('add-comment'); $comment_output .= ' <textarea name="comment_text" style="width:97%"></textarea> <input type="hidden" name="order_id" value="' . $order->id . '"> <input type="hidden" name="action" value="add_comment"> <input class="btn btn-large btn-block" type="submit" name="submit_comment" value="' . __('Add comment', 'wcvendors') . '">'; } $comment_output .= '</div>'; } $order_items = array(); $order_items['order_id'] = $order->id; $order_items['customer'] = $order->get_formatted_shipping_address(); $order_items['products'] = $products; $order_items['total'] = woocommerce_price($total); $order_items['date'] = date_i18n(wc_date_format(), strtotime($order->order_date)); // $order_items[ 'comments' ] = $comment_output; $order_items['status'] = $shipped; $orders[] = (object) $order_items; $model_id++; } } return $orders; }
/** * Sum of orders for a specific order * * @param array $order_ids * @param array $args (optional) * * @return object */ public static function sum_for_orders(array $order_ids, array $args = array()) { global $wpdb; $dates = WCV_Queries::orders_within_range(); $defaults = array('status' => apply_filters('wcvendors_completed_statuses', array('completed', 'processing')), 'dates' => array('before' => $dates['before'], 'after' => $dates['after'])); $args = wp_parse_args($args, $defaults); $sql = "\n\t\t\tSELECT COUNT(order_id) as total_orders,\n\t\t\t SUM(total_due + total_shipping + tax) as line_total,\n\t\t\t SUM(qty) as qty,\n\t\t\t product_id\n\n\t\t\tFROM {$wpdb->prefix}pv_commission\n\n\t\t\tWHERE order_id IN ('" . implode("','", $order_ids) . "')\n\t\t"; if (!empty($args['dates']['after'])) { $sql .= "\n\t\t\t\tAND time >= '" . $args['dates']['after'] . "'\n\t\t\t"; } if (!empty($args['dates']['before'])) { $sql .= "\n\t\t\t\tAND time <= '" . $args['dates']['before'] . "'\n\t\t\t"; } $sql .= "\n\t\t\tAND status != 'reversed'\n\t\t"; if (!empty($args['vendor_id'])) { $sql .= "\n\t\t\t\tAND vendor_id = {$args['vendor_id']}\n\t\t\t"; } $sql .= "\n\t\t\tGROUP BY order_id\n\t\t\tORDER BY time DESC;\n\t\t"; $orders = $wpdb->get_results($sql); return $orders; }
} $shippers = (array) get_post_meta($order->id, 'wc_pv_shipped', true); $shipped = in_array($user_id, $shippers); ?> <tr> <td><?php echo $order->get_order_number(); ?> </td> <td><?php echo apply_filters('wcvendors_dashboard_google_maps_link', '<a target="_blank" href="' . esc_url('http://maps.google.com/maps?&q=' . urlencode(esc_html(preg_replace('#<br\\s*/?>#i', ', ', $order->get_formatted_shipping_address()))) . '&z=16') . '">' . esc_html(preg_replace('#<br\\s*/?>#i', ', ', $order->get_formatted_shipping_address())) . '</a>'); ?> </td> <td><?php $sum = WCV_Queries::sum_for_orders(array($order->id), array('vendor_id' => get_current_user_id())); $total = $sum[0]->line_total; $totals += $total; echo woocommerce_price($total); ?> </td> <td><?php echo $order->order_date; ?> </td> <td> <a href="#" class="view-items" id="<?php echo $order->id; ?> "><?php _e('View items', 'wcvendors');
function get_orders() { $user_id = get_current_user_id(); $orders = array(); $vendor_products = WCV_Queries::get_commission_products($user_id); $products = array(); foreach ($vendor_products as $_product) { $products[] = $_product->ID; } $_orders = WCV_Queries::get_orders_for_products($products); if (!empty($_orders)) { foreach ($_orders as $order) { $order = new WC_Order($order->order_id); $valid_items = WCV_Queries::get_products_for_order($order->id); $valid = array(); $items = $order->get_items(); foreach ($items as $key => $value) { if (in_array($value['variation_id'], $valid_items) || in_array($value['product_id'], $valid_items)) { $valid[] = $value; } } $products = ''; foreach ($valid as $key => $item) { $item_meta = new WC_Order_Item_Meta($item['item_meta']); // $item_meta = $item_meta->display( false, true ); $item_meta = $item_meta->get_formatted(); $products .= '<strong>' . $item['qty'] . ' x ' . $item['name'] . '</strong><br />'; foreach ($item_meta as $key => $meta) { // Remove the sold by meta key for display if (strtolower($key) != 'sold by') { $products .= $meta['label'] . ' : ' . $meta['value'] . '<br />'; } } } $shippers = (array) get_post_meta($order->id, 'wc_pv_shipped', true); $shipped = in_array($user_id, $shippers) ? 'Yes' : 'No'; $sum = WCV_Queries::sum_for_orders(array($order->id), array('vendor_id' => get_current_user_id())); $total = $sum[0]->line_total; $order_items = array(); $order_items['order_id'] = $order->id; $order_items['customer'] = $order->get_formatted_shipping_address(); $order_items['products'] = $products; $order_items['total'] = woocommerce_price($total); $order_items['date'] = date_i18n(wc_date_format(), strtotime($order->order_date)); $order_items['status'] = $shipped; $orders[] = (object) $order_items; } } return $orders; }
/** * Get Orders to display in admin * * @return $orders */ function get_orders() { $user_id = get_current_user_id(); $orders = array(); $vendor_products = $this->get_vendor_products($user_id); $products = array(); foreach ($vendor_products as $_product) { $products[] = $_product->ID; } $_orders = $this->get_orders_for_vendor_products($products); $model_id = 0; if (!empty($_orders)) { foreach ($_orders as $order) { $order = new WC_Order($order->order_id); $valid_items = WCV_Queries::get_products_for_order($order->id); $valid = array(); $items = $order->get_items(); foreach ($items as $key => $item) { if (in_array($item['variation_id'], $valid_items) || in_array($item['product_id'], $valid_items)) { $valid[] = $item; } } $products = ''; foreach ($valid as $key => $item) { $wc_product = new WC_Product($item['product_id']); $products .= '<strong>' . $item['qty'] . ' x ' . $item['name'] . '</strong><br />'; if ($metadata = $order->has_meta($item['product_id'])) { $products .= '<table cellspacing="0" class="wcv_display_meta">'; foreach ($metadata as $meta) { // Skip hidden core fields if (in_array($meta['meta_key'], apply_filters('woocommerce_hidden_order_itemmeta', array('_qty', '_tax_class', '_product_id', '_variation_id', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', WC_Vendors::$pv_options->get_option('sold_by_label'))))) { continue; } // Skip serialised meta if (is_serialized($meta['meta_value'])) { continue; } // Get attribute data if (taxonomy_exists(wc_sanitize_taxonomy_name($meta['meta_key']))) { $term = get_term_by('slug', $meta['meta_value'], wc_sanitize_taxonomy_name($meta['meta_key'])); $meta['meta_key'] = wc_attribute_label(wc_sanitize_taxonomy_name($meta['meta_key'])); $meta['meta_value'] = isset($term->name) ? $term->name : $meta['meta_value']; } else { $meta['meta_key'] = apply_filters('woocommerce_attribute_label', wc_attribute_label($meta['meta_key'], $wc_product), $meta['meta_key']); } $products .= '<tr><th>' . wp_kses_post(rawurldecode($meta['meta_key'])) . ':</th><td>' . wp_kses_post(wpautop(make_clickable(rawurldecode($meta['meta_value'])))) . '</td></tr>'; } $products .= '</table>'; } } $shippers = (array) get_post_meta($order->id, 'wc_pv_shipped', true); $shipped = in_array($user_id, $shippers) ? 'Yes' : 'No'; $sum = WCV_Queries::sum_for_orders(array($order->id), array('vendor_id' => get_current_user_id())); $total = $sum[0]->line_total; $comment_output = ''; // Need to fix how form is submitted for adding comments if at all possible. // if ( $this->can_view_comments) { // $order_notes = $order->get_customer_order_notes(); // $comment_output .= '<a href="#TB_inline?width=600&height=550&inlineId=order-comment-window-'.$model_id.'" class="thickbox">'; // $comment_output .= sprintf( __( 'Comments (%s)', 'wcvendors' ), count( $order_notes ) ); // $comment_output .= '</a>'; // $comment_output .= '<div id="order-comment-window-'.$model_id.'" style="display:none;">'; // $comment_output .= '<h3>'.__('Comments to Customer', 'wcvendors' ). '</h3>'; // if ( !empty( $order_notes ) ) { // foreach ($order_notes as $order_note) { // $last_added = human_time_diff( strtotime( $order_note->comment_date_gmt ), current_time( 'timestamp', 1 ) ); // $comment_output .= '<p>'; // $comment_output .= $order_note->comment_content; // $comment_output .= '<br />'; // $comment_output .= sprintf(__( 'added %s ago', 'wcvendors' ), $last_added ); // $comment_output .= '<br />'; // $comment_output .= '</p>'; // } // } else { // $comment_output .= '<p>'.__('No comments currently to customer.', 'wcvendors' ). '</p>'; // } // if ( $this->can_add_comments ) { // $comment_output .= wp_nonce_field( 'add-comment' ); // $comment_output .= ' // <textarea name="comment_text" style="width:97%"></textarea> // <input type="hidden" name="order_id" value="'. $order->id .'"> // <input type="hidden" name="action" value="add_comment"> // <input class="btn btn-large btn-block" type="submit" name="submit_comment" value="'.__( 'Add comment', 'wcvendors' ).'">'; // } // $comment_output .= '</div>'; // } $order_items = array(); $order_items['order_id'] = $order->id; $order_items['customer'] = $order->get_formatted_shipping_address(); $order_items['products'] = $products; $order_items['total'] = woocommerce_price($total); $order_items['date'] = date_i18n(wc_date_format(), strtotime($order->order_date)); // $order_items[ 'comments' ] = $comment_output; $order_items['status'] = $shipped; $orders[] = (object) $order_items; $model_id++; } } return $orders; }