/** * Submit a comment for an order * * @param object $orders * * @return unknown */ public static function new_comment($orders) { global $woocommerce; $user = wp_get_current_user(); $user = $user->ID; // Security if (!wp_verify_nonce($_POST['_wpnonce'], 'add-comment')) { return false; } // Check if this product belongs to the vendor submitting the comment $product_id = (int) $_POST['product_id']; $author = PV_Vendors::get_vendor_from_product($product_id); if ($author != $user) { return false; } // Find the order belonging to this comment foreach ($orders as $order) { if ($order->order_id == $_POST['order_id']) { $found_order = $order; break; } } // No order was found if (empty($found_order)) { return false; } // Don't submit empty comments if (empty($_POST['comment_text'])) { if (function_exists('wc_add_error')) { wc_add_error(__('You\'ve left the comment field empty!', 'wc_product_vendor')); } else { $woocommerce->add_error(__('You\'ve left the comment field empty!', 'wc_product_vendor')); } return false; } // Only submit if the order has the product belonging to this vendor $found_order = new WC_Order($found_order->order_id); $valid_order = false; foreach ($found_order->get_items() as $item) { if ($item['product_id'] == $product_id) { $valid_order = true; break; } } if ($valid_order) { $comment = esc_textarea($_POST['comment_text']); add_filter('woocommerce_new_order_note_data', array(__CLASS__, 'filter_comment'), 10, 2); $found_order->add_order_note($comment, 1); remove_filter('woocommerce_new_order_note_data', array(__CLASS__, 'filter_comment'), 10, 2); if (function_exists('wc_add_message')) { wc_add_message(__('Success. The customer has been notified of your comment.', 'wc_product_vendor')); } else { $woocommerce->add_message(__('Success. The customer has been notified of your comment.', 'wc_product_vendor')); } } }
/** * * * @param unknown $order * @param unknown $group (optional) * * @return unknown */ public function get_vendor_dues_from_order($order, $group = true) { global $woocommerce; $give_tax = Product_Vendor::$pv_options->get_option('give_tax'); $receiver = array(); $shipping_given = 0; $tax_given = 0; foreach ($order->get_items() as $key => $product) { $product_id = !empty($product['variation_id']) ? $product['variation_id'] : $product['product_id']; $author = PV_Vendors::get_vendor_from_product($product_id); $is_vendor = PV_Vendors::is_vendor($author); $commission = $is_vendor ? PV_Commission::calculate_commission($product['line_subtotal'], $product_id, $order) : 0; $tax = !empty($product['line_tax']) ? (double) $product['line_tax'] : 0; $shipping = PV_Shipping::get_shipping_due($order->id, $product, $author); if ($is_vendor) { $shipping_given += $shipping; $tax_given += $give_tax ? $tax : 0; $give = 0; $give += !empty($receiver[$author]['total']) ? $receiver[$author]['total'] : 0; $give += $shipping; $give += $commission; $give += $give_tax ? $tax : 0; if ($group) { $receiver[$author] = array('vendor_id' => (int) $author, 'commission' => !empty($receiver[$author]['commission']) ? $receiver[$author]['commission'] + $commission : $commission, 'shipping' => !empty($receiver[$author]['shipping']) ? $receiver[$author]['shipping'] + $shipping : $shipping, 'tax' => $give_tax ? !empty($receiver[$author]['tax']) ? $receiver[$author]['tax'] + $tax : $tax : 0, 'qty' => !empty($receiver[$author]['qty']) ? $receiver[$author]['qty'] + $product['qty'] : $product['qty'], 'total' => $give); } else { $receiver[$author][$product_id] = array('vendor_id' => (int) $author, 'product_id' => $product_id, 'commission' => $commission, 'shipping' => $shipping, 'tax' => $give_tax ? $tax : 0, 'qty' => $product['qty'], 'total' => $shipping + $commission + ($give_tax ? $tax : 0)); } } $admin_comm = $product['line_subtotal'] - $commission; if ($group) { $receiver[1] = array('vendor_id' => 1, 'qty' => !empty($receiver[1]['qty']) ? $receiver[1]['qty'] + $product['qty'] : $product['qty'], 'commission' => !empty($receiver[1]['commission']) ? $receiver[1]['commission'] + $admin_comm : $admin_comm, 'total' => !empty($receiver[1]) ? $receiver[1]['total'] + $admin_comm : $admin_comm); } else { $receiver[1][$product_id] = array('vendor_id' => 1, 'product_id' => $product_id, 'commission' => $admin_comm, 'shipping' => 0, 'tax' => 0, 'qty' => $product['qty'], 'total' => $admin_comm); } } // Add remainders on end to admin $discount = $order->get_total_discount(); $shipping = $order->order_shipping - $shipping_given; $tax = $order->order_tax + $order->order_shipping_tax - $tax_given; $total = $tax + $shipping - $discount; if ($group) { $receiver[1]['commission'] = $receiver[1]['commission'] - $discount; $receiver[1]['shipping'] = $shipping; $receiver[1]['tax'] = $tax; $receiver[1]['total'] += $total; } else { $receiver[1][$product_id]['commission'] = $receiver[1][$product_id]['commission'] - $discount; $receiver[1][$product_id]['shipping'] = $order->order_shipping - $shipping_given; $receiver[1][$product_id]['tax'] = $tax; $receiver[1][$product_id]['total'] += $total; } // Reset the array keys // $receivers = array_values( $receiver ); return $receiver; }
/** * * * @param unknown $items * @param unknown $order * * @return unknown */ public function check_items($items, $order) { foreach ($items as $key => $product) { if (empty($product['product_id'])) { unset($items[$key]); continue; } $author = PV_Vendors::get_vendor_from_product($product['product_id']); if ($this->current_vendor != $author) { unset($items[$key]); continue; } else { $commission_due = PV_Commission::calculate_commission($product['line_subtotal'], $product['product_id'], $order); $items[$key]['line_subtotal'] = $commission_due; $items[$key]['line_total'] = $commission_due; unset($items[$key]['line_tax']); } } return $items; }